UITableViewCell重用良好的做法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NotificationViewCell"; CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell==nil){ NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; cell.contentView.backgroundColor=[UIColor clearColor]; [ModelClass addSublayer:cell.contentView]; cell.cellbg.layer.cornerRadius = 8; cell.cellbg.layer.masksToBounds = YES; cell.cellbg.layer.borderWidth = 1; cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor]; cell.logoImage.layer.cornerRadius = 8; cell.logoImage.layer.masksToBounds = YES; cell.logoImage.layer.borderWidth = 1; cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor]; Merchant *merchantList= [self.cardListArr objectAtIndex:indexPath.row]; cell.nameLab.text=merchantList.name; NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points); // NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO); cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]]; cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO]; if(![ModelClass isBlankString:merchantList.introPic]) { NSLog(@"merchantList.introPic=%@",merchantList.introPic); [cell.logoImage setImageUrl:merchantList.introPic]; } } return cell; } 
  1. 我想使用上面的代码重用UITableViewCell,我不知道它是否正确,我想得到一些建议。
  2. 你可以看到我使用的代码if(cell == nil),我想知道我应该编写什么代码if(cell!= nil)(if(cell == nil)else {我应该做什么可以改善单元重用})

  3. 如果每个单元格都有相同的视图,但有不同的高度,例如imageview有时是20或40等等,如何处理这种情况。

1.这是不正确的,因为你的单元被重用了,当它被创建时,它不会进入if-statement ,因此,在if-statement你只需要初始化单元格,即setText和setImage你应该在if-statement之外编码。

如 :

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NotificationViewCell"; CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell==nil){ NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; cell.contentView.backgroundColor=[UIColor clearColor]; [ModelClass addSublayer:cell.contentView]; cell.cellbg.layer.cornerRadius = 8; cell.cellbg.layer.masksToBounds = YES; cell.cellbg.layer.borderWidth = 1; cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor]; cell.logoImage.layer.cornerRadius = 8; cell.logoImage.layer.masksToBounds = YES; cell.logoImage.layer.borderWidth = 1; cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor]; } Merchant *merchantList= [self.cardListArr objectAtIndex:indexPath.row]; cell.nameLab.text=merchantList.name; NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points); // NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO); cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]]; cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO]; if(![ModelClass isBlankString:merchantList.introPic]) { NSLog(@"merchantList.introPic=%@",merchantList.introPic); [cell.logoImage setImageUrl:merchantList.introPic]; } return cell; } 

2大多数人的代码如下:

 if(cell==nil) { //init code } // setting code 

3.如果要设置单元格高度,则无法在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath编写代码- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

你应该在dataSource的方法中编码: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

像这样的代码:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (index.row % 2) return 20.0f; return 40.0f; } 

我通常将单元的创建和配置分为两个逻辑部分:

  1. 创建单元格并设置每个单元格 (即布局,图层) 相同的所有属性
  2. 写一个单独的-(void)configureCell:(UITableViewCell*)cell; function,我从数据源设置特定单元格的所有内容(即标签和imageView的值)。

然后在cellForRowAtIndexPath

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NotificationViewCell"; CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell==nil){ [self createCell:&cell]; } Mercant* mercantList = [self.cardListArr objectAtIndex:indexPath.row]; [self configureCell:cell withMercant:mercantList]; return cell; } -(void)createCell:(CardViewCell**)cellPtr { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil]; *cellPtr = [nib objectAtIndex:0]; CardViewCell* cell = *cellPtr; cell.contentView.backgroundColor=[UIColor clearColor]; [ModelClass addSublayer:cell.contentView]; cell.cellbg.layer.cornerRadius = 8; cell.cellbg.layer.masksToBounds = YES; cell.cellbg.layer.borderWidth = 1; cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor]; cell.logoImage.layer.cornerRadius = 8; cell.logoImage.layer.masksToBounds = YES; cell.logoImage.layer.borderWidth = 1; cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor]; } -(void)configureCell:(CardViewCell*)cell withMercant:(Mercant*)mercantList { cell.nameLab.text=merchantList.name; NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points); // NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO); cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]]; cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO]; if(![ModelClass isBlankString:merchantList.introPic]) { NSLog(@"merchantList.introPic=%@",merchantList.introPic); [cell.logoImage setImageUrl:merchantList.introPic]; } } 

郭禄川的上述答案工作正常,但问题是每次上下滚动时都会重新创建单元格,如果你有不同状态的UIComponents,你可能很容易丢失这些状态。 此外,我建议您缓存所有创建的单元格并重复使用它们以避免这种情况。

以下是缓存如何工作的示例。

 var mockCells = [QuestionsCell](repeating: QuestionsCell(), count: 1000) 

*你的cellForRowAtIndexPath里面放了以下*

  if cell == indexPath.row { //if cell exists in our list we reuse cell = mockCell } else { //here we store/cache each created cell mockCells[indexPath.row] = cell } }