2010-03-15 58 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