开发者

Customizing table cells . Adding images and labels

I've a table thats displaying some data. There are some images drawn. And lots of labels. I was wondering is there a better way or am I doing something wrong? Any advice or tips welcome.

- (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;
}

Also these cells should not allow the user to interact with them. The table is just able to scroll.

Thanks -Code

EDIT

Ok is this the basic shell that cellForRowAtIndexPath: should have?

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

if (cell==nil)
{
 // we dn开发者_开发知识库t have this cell made yet so we need to alloc a new cell
 // and do some code to add data to the cell labels, images, buttons etc
}
else
{
// we already made this cell earlier so there is nothing to do just return the cell
}

// dnt be adding anything to the cell here because in the case the cell already exsists 
// you will just draw over stuff already in the cell
return cell;
}

Is that pretty much it?

Thanks again Code


your code doesn't reuse cells. This is not good, your tableview will be slow.

do it like this:

- (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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜