Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 自定义表格单元格。添加图像和标签_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 自定义表格单元格。添加图像和标签

Iphone 自定义表格单元格。添加图像和标签,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我有一张表,上面显示了一些数据。 画了一些图像。还有很多标签。 我想知道有没有更好的方法,或者我做错了什么?欢迎提供任何建议或提示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableV

我有一张表,上面显示了一些数据。 画了一些图像。还有很多标签。 我想知道有没有更好的方法,或者我做错了什么?欢迎提供任何建议或提示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"Cell";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

//scores not ready yet so do not try and draw them
if (initializeScoresHappening)
{
    UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(105,0, 100, 20)];
    teamVersusLabel.textAlignment = UITextAlignmentCenter;
    teamVersusLabel.textColor =  [UIColor redColor];
    teamVersusLabel.backgroundColor = [UIColor clearColor];
    [teamVersusLabel setFont:[UIFont fontWithName:@"Marker Felt" size:14]];
    teamVersusLabel.text = @"Initialising score data...";
    [cell addSubview:teamVersusLabel];
    [teamVersusLabel release];
}
else 
{
    //Draw the default cell background image for a normal sized cell
    UIImageView *backGroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableCellBackground.png"]];
    [cell addSubview:backGroundImage];

    int cellHeight = [self cellHeight:indexPath.row];

    int numberOfExtensionsNeeded = (cellHeight - TABLECELLBACKGROUNDHEIGHT) / TABLECELLBACKGROUNDEXTENSIONHEIGHT;

    // if cell is bigger than the default sized background image draw in extension images
    if (numberOfExtensionsNeeded > 0)
    {
        int i;
        for (i=TABLECELLBACKGROUNDHEIGHT; i<(cellHeight-TABLECELLBACKGROUNDEXTENSIONHEIGHT); i+=TABLECELLBACKGROUNDEXTENSIONHEIGHT)
        {
            UIImageView *backGroundImageExtension = [[UIImageView alloc] initWithFrame:CGRectMake(0, i, CELLWIDTH, TABLECELLBACKGROUNDEXTENSIONHEIGHT)];
            [backGroundImageExtension setImage:[UIImage imageNamed:@"tableCellBackgroundExtension.png"] ]; 
            [cell addSubview:backGroundImageExtension];
            [backGroundImageExtension release];                                      
        }

        int remainingArena = (cellHeight - i);
        if (remainingArena > 0 )
        {
            int yPos = i - (TABLECELLBACKGROUNDEXTENSIONHEIGHT -remainingArena);
            UIImageView *backGroundImageExtension = [[UIImageView alloc] initWithFrame:CGRectMake(0, yPos, CELLWIDTH, TABLECELLBACKGROUNDEXTENSIONHEIGHT)];
            [backGroundImageExtension setImage:[UIImage imageNamed:@"tableCellBackgroundExtension.png"] ]; 
            [cell addSubview:backGroundImageExtension];                                          
            [backGroundImageExtension release]; 
        }
    }
    else 
    {

        int i = (cellHeight - TABLECELLBACKGROUNDEXTENSIONHEIGHT);
        UIImageView *backGroundImageExtension = [[UIImageView alloc] initWithFrame:CGRectMake(0, i, CELLWIDTH, TABLECELLBACKGROUNDEXTENSIONHEIGHT)];
        [backGroundImageExtension setImage:[UIImage imageNamed:@"tableCellBackgroundExtension.png"] ]; 
        [cell addSubview:backGroundImageExtension];
        [backGroundImageExtension release];         

    }

    // Get the match object that holds the game data
    Match *aMatch = [appDelegate.matchResultsArray objectAtIndex:indexPath.row];

    UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,0, 145, 20)];
    teamName1Label.textAlignment = UITextAlignmentCenter;
    teamName1Label.textColor =  [UIColor redColor];
    teamName1Label.backgroundColor = [UIColor clearColor];
    [teamName1Label setFont:[UIFont fontWithName:@"Marker Felt" size:14]];
    teamName1Label.text = aMatch.teamName1;
    [cell addSubview:teamName1Label];
    [teamName1Label release];

    UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(145,0, 20, 20)];
    teamVersusLabel.textAlignment = UITextAlignmentCenter;
    teamVersusLabel.textColor =  [UIColor redColor];
    teamVersusLabel.backgroundColor = [UIColor clearColor];
    [teamVersusLabel setFont:[UIFont fontWithName:@"Marker Felt" size:14]];
    teamVersusLabel.text = @"v";
    [cell addSubview:teamVersusLabel];
    [teamVersusLabel release];

    UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(160,0, 145, 20)];
    teamName2Label.textAlignment = UITextAlignmentCenter;
    teamName2Label.textColor =  [UIColor redColor];
    teamName2Label.backgroundColor = [UIColor clearColor];
    [teamName2Label setFont:[UIFont fontWithName:@"Marker Felt" size:14]];
    teamName2Label.text = aMatch.teamName2;
    [cell addSubview:teamName2Label];
    [teamName2Label release];

    UILabel *theScoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(115, 20, 80, 20)];
    theScoreLabel.textAlignment = UITextAlignmentCenter;
    theScoreLabel.textColor =  [UIColor redColor];
    theScoreLabel.backgroundColor = [UIColor clearColor];
    [theScoreLabel setFont:[UIFont fontWithName:@"Marker Felt" size:14]];
    theScoreLabel.text = aMatch.theScore;
    [cell addSubview:theScoreLabel];
    [theScoreLabel release];

    int xVal = 0;
    int yVal = 0;
    int numTeam1Scores = 0;
    int numTeam2Scores = 0;


    for(int i=0; i<aMatch.matchGoalCount; i++)
    {
        NSString *teamOneOrTwo = [aMatch.scorerOrderArray objectAtIndex:i];
        if ([teamOneOrTwo compare:@"1" ] == NSOrderedSame)
        {
            //draw team 1 goal scorer on left side
            yVal = 40+(numTeam1Scores*20);
            xVal = 5;
            numTeam1Scores++;
        }
        else if ([teamOneOrTwo compare:@"2" ] == NSOrderedSame )
        {
            //draw team 2 goal scorer on right side
            yVal = 40+(numTeam2Scores*20);
            xVal = 160;
            numTeam2Scores++;   
        }
        else
        {
            //error -.-
            break;
        }

        UILabel *theScorerLabel = [[UILabel alloc]initWithFrame:CGRectMake(xVal, yVal, 145, 20)];
        if (xVal < 50)
        {
            //NSLog(@" UITextAlignmentLeft ");
            theScorerLabel.textAlignment = UITextAlignmentLeft;
        }
        else
        {
            //NSLog(@" UITextAlignmentRight ");
            theScorerLabel.textAlignment = UITextAlignmentRight;
        }

        theScorerLabel.textColor =  [UIColor blackColor];
        theScorerLabel.backgroundColor = [UIColor clearColor];
        [theScorerLabel setFont:[UIFont fontWithName:@"Marker Felt" size:12]];
        theScorerLabel.text = [aMatch.scorersArray objectAtIndex:i];
        [cell addSubview:theScorerLabel];
    }
}
return cell;
}
差不多了吗

再次感谢
代码

您的代码不会重用单元格。这不好,您的tableview将很慢

这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    // try to dequeue a cell from the tableview, for reuse:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // dequeue failed
    if (cell == nil) {
        // create new cell:
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        // you add your subviews here.
        // example:

        UILabel *theScoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(115, 20, 80, 20)];
        theScoreLabel.tag = 42;
        theScoreLabel.textAlignment = UITextAlignmentCenter;
        theScoreLabel.textColor =  [UIColor redColor];
        theScoreLabel.backgroundColor = [UIColor clearColor];
        [theScoreLabel setFont:[UIFont fontWithName:@"Marker Felt" size:14]];

        // add to the contentView!!!
        [cell.contentView addSubview:theScoreLabel];
        [theScoreLabel release];
    }
    else {
        // Nothing to do here. Because in either way we change the values of the cell later.
    }
    // whatever happened before, we have a valid cell at this point.

    // you configure your subviews here.
    // example:

    Match *aMatch = [appDelegate.matchResultsArray objectAtIndex:indexPath.row];

    UILabel *theScoreLabel = (UILabel *)[cell.contentView viewWithTag:42];
    theScoreLabel.text = aMatch.theScore;


    return cell;
}

嗯,有点困惑!如果cell等于nil,那么在cell中绘制内容,在这种情况下是否应该有一个else,而不是nil,只返回cell?也许我不明白那里发生了什么?这是基本的tableview知识,也许你应该后退一步,用标准单元格玩多一点。无论如何,我在代码中添加了一些注释。如果cell等于nil,则意味着无法检索具有CellIdentifier的单元格,这意味着它需要在if分支中创建一个,您将分配并初始化自定义单元格。之后,您可以将数据发送到单元格以显示数据。else分支不是必需的,因为您将始终在if语句中重复使用或创建一个cell分支
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    // try to dequeue a cell from the tableview, for reuse:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // dequeue failed
    if (cell == nil) {
        // create new cell:
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        // you add your subviews here.
        // example:

        UILabel *theScoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(115, 20, 80, 20)];
        theScoreLabel.tag = 42;
        theScoreLabel.textAlignment = UITextAlignmentCenter;
        theScoreLabel.textColor =  [UIColor redColor];
        theScoreLabel.backgroundColor = [UIColor clearColor];
        [theScoreLabel setFont:[UIFont fontWithName:@"Marker Felt" size:14]];

        // add to the contentView!!!
        [cell.contentView addSubview:theScoreLabel];
        [theScoreLabel release];
    }
    else {
        // Nothing to do here. Because in either way we change the values of the cell later.
    }
    // whatever happened before, we have a valid cell at this point.

    // you configure your subviews here.
    // example:

    Match *aMatch = [appDelegate.matchResultsArray objectAtIndex:indexPath.row];

    UILabel *theScoreLabel = (UILabel *)[cell.contentView viewWithTag:42];
    theScoreLabel.text = aMatch.theScore;


    return cell;
}