2010-03-15 59 views
0

我想知道是否可以添加單元格內容到基於行索引的uitableview?基於行索引的自定義單元格iphone

例如,如果它是第一個單元格(第0行),我想添加圖像和文本。 如果是第二行,我想添加一個按鈕。 對於其他所有行,我只想添加一行文本。

我試過使用indexPath.row。它工作的第一次,但是當我滾動屏幕,然後備份我的第一個圖像消失。

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

    PromoListItem *promo = [promoList objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"Cell"; 

    AsyncImageView *asyncImageView = nil; 
    UILabel *label = nil; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(indexPath.row==0) 
    { 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     CGRect frame; 
     frame.origin.x = 0; 
     frame.origin.y = 0; 
     frame.size.width = 100; 
     frame.size.height = 100; 

     asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 
     asyncImageView.tag = ASYNC_IMAGE_TAG; 
     [cell.contentView addSubview:asyncImageView]; 


     frame.origin.x = 110; 
     frame.size.width =100; 
     label = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
     label.tag = LABEL_TAG; 
     [cell.contentView addSubview:label]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } else { 
     asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG]; 
     label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG]; 
    } 

    NSURL *url = [NSURL URLWithString:promo.imgurl]; 

    [asyncImageView loadImageFromURL:url]; 

    label.text = promo.artistname; 
    }else 

    { 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      CGRect frame; 
      frame.origin.x = 0; 
      frame.origin.y = 0; 
      frame.size.width = 100; 
      frame.size.height = 100; 

      //asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease]; 
     // asyncImageView.tag = ASYNC_IMAGE_TAG; 
     // [cell.contentView addSubview:asyncImageView]; 


      frame.origin.x = 110; 
      frame.size.width =100; 
      label = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
      label.tag = LABEL_TAG; 
      [cell.contentView addSubview:label]; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } else { 
     // asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG]; 
      label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG]; 
     } 

     //NSURL *url = [NSURL URLWithString:promo.imgurl]; 

     //[asyncImageView loadImageFromURL:url]; 

     label.text = promo.artistname; 



    } 

    return cell; 
} 

回答

0

如何定製你想要這些細胞?如果只添加文本,圖像和附件按鈕,則可以在行檢查之外創建單元格,然後在這些if語句內添加所需的項目。

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    NSUInteger row = [indexPath row]; 
    switch(row) { 
     case 0: 
      cell.textLabel.text = @"row 0"; 
      cell.image 
      break; 
     case 1: 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      break; 
     default: 
      cell.textLabel.text = [NSString stringWithFormat:@"row %d",row]; 
      break;  
    } 

    return cell; 

然而,行,當你開始回收你的細胞會發生變化,你可能想在指示電池應該如何工作的實際數據的東西。

+0

謝謝。我想我會放棄這種方法。我最初想要6個電池。單元格0有一個圖像和一些大標籤,另外5個只有文本。 它應用程序第一次加載,但一旦你開始滾動圖像從0行消失,它的一部分結束了在單元格6中。相反,我可能只是添加一個UIImage和一個tableview與5單元格到UIViewController而不是試圖在UITableViewController – dubbeat 2010-03-15 15:44:54

+0

中做這一切是的,如果你使用的是少於10個的單元,甚至不用擔心重新安裝它們。 – 2010-03-15 15:49:04