2011-01-31 21 views
0

私はいくつかのデータを表示しているテーブルを持っています。 一部の画像が描画されています。そしてたくさんのラベル。 もっと良い方法があるのだろうか、間違っているのだろうか?アドバイスやヒントを歓迎します。テーブルセルのカスタマイズ。画像とラベルの追加

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

また、これらのセルでユーザーが対話できないようにする必要があります。テーブルはスクロールできるだけです。持っている必要があります。これはcellForRowAtIndexPathが、基本的なシェル

おかげ -code

EDIT

OKですか?

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

if (cell==nil) 
{ 
// we dnt 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; 
} 

これはかなりですか?

おかげで再び コード

答えて

0

あなたのコードでは、細胞を再利用しません。これは良くありません、あなたのテーブルビューは遅くなります。

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

うーん少し混乱:

はこのようにそれを行います!セルがnilに等しい場合、セルに要素が描画され、ケースにelseが存在しない場合は、セルが返されません。たぶん私はそこに起こっていることに従いませんか? –

+0

これは基本的なテーブルビューの知識です。多分、あなたは一歩前進して、 "標準"のセルでもう少し遊ぶべきです。とにかく、コードにいくつかのコメントを追加しました。 –

+0

cellがnilに等しい場合は、CellIdentifierを持つセルを取得できなかったことを意味します。つまり、ifブランチ内でカスタムセルを割り当てて初期化します。その後、データをセルに送信して表示することができます。 elseブランチは必要ありません。なぜなら、常に(再利用された、またはif文で作成された)セルがあるからです。 –