Ios 为什么customTableViewCell不同且速度慢?

Ios 为什么customTableViewCell不同且速度慢?,ios,uitableview,Ios,Uitableview,在故事板中,我设置了tableviewcell标识符“firstCell”和类“FirstTableViewCell”。 在UIViewcontroller中: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"firstCell"; FirstTable

在故事板中,我设置了tableviewcell标识符“firstCell”和类“FirstTableViewCell”。
在UIViewcontroller中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"firstCell";
   FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
   NSLog(@"%@",cell);
   if (cell == nil)
   {
       cell = (FirstTableViewCell *)[[FirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
   }
    return cell;
}
输出:

2013-09-12 20:30:10.378 InfoDrop[4120:c07] FirstTableViewCell: 0x75624b0; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = CALayer : 0x7562610    

2013-09-12 20:30:10.584 InfoDrop[4120:c07] FirstTableViewCell: 0x75624b0; baseClass = UITableViewCell; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = CALayer: 0x7562610

2013-09-12 20:30:10.586 InfoDrop[4120:c07] <FirstTableViewCell: 0x8181a40; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x81a46e0>>

2013-09-12 21:18:08.042 InfoDrop[4415:c07] <FirstTableViewCell: 0x83993e0; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x8398c90>>

我强烈建议您在第一个TableViewCell类中实现prepareForReuse方法。在该方法中,清除单元无法重用的所有元素(例如:UILabel文本值)。我不建议用这种方法重新创建UIView,因为性能可能会受到影响

但是,如果不查看与此问题相关的执行堆栈中的全部代码,就很难给出完整的判断

更新 基于您的代码更新,我建议您执行以下操作

//In your .h cell file

@property(nonatomic,strong)UILabel *nameLabelView
@property(nonatomic,strong)UILabel *detailLabelView
@property(nonatomic,strong)UIView *leftView

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
      // Initialization code
      //Here initialize your uilabels and uiview using CGRectZero as frame
      self.leftView = [UIImageView alloc] initWithFrame:CGRectMake(0,0.0,27.0,27.0)];
      [self.contentView addSubview:self.leftView];
  }
  return self;

  -(void)prepareForReuse
  {
    //clean up the text values for your labels
  }

 -(void)setCa:(Categories *)ca{
    UILabel *nameLabelView = (UILabel *)[self.contentView viewWithTag:1];
    nameLabelView.text=ca.name;

    UILabel *detailLabelView = (UILabel *)[self.contentView viewWithTag:2];
    detailLabelView.text=[ca.count stringValue];
    UIView *fill;
    switch (tmp) {
      case 1:
      fill = [[CreditView alloc]init];         
       break;
       case 2:
       fill = [[BankView alloc]init];
       break;
       case 3:
       fill = [[QuestionView alloc]init];
       break;
       case 4:
       fill = [[SoftwareView alloc]init];
       break;
       case 5:
       fill = [[NoteView alloc]init];
       break;
       default:
       break;
    }
        [self.lefView addSubview:fill];
        //BTW this code has a lot of potential for a good refactoring
 }

 -(void)layoutSubViews{
      self.leftView.frame = CGRectMake(0.0,0.0,27.0,27.0);
      //Do the rest of positioning for your elements here
 }
对于这种情况,请拨打:

[left setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];

因为您强制系统重新绘制您的视图,所以这是一种过度杀伤力。而是在布局子视图中进行定位,每当单元格出现在表中时,uiTableViewController都会调用该视图

您的
FirstTableViewCell
是否有一个XIB文件?从这个自定义单元格的initWithStyle粘贴您的代码-您在那里做什么?从您的输出来看,前两个调用似乎正在使用单元(相同的内存地址0x75624b0),第三个调用正在创建干净的单元。您的类中是否也实现了PrepareForuse?@GrzegorzKrukowski粘贴了cell的部分代码。我没有准备。我把2个UILabel和一个uiview使用情节提要。@Amar没有XIB文件都放在情节提要中,这是您得到的全部日志输出吗?奇怪的是,前两个单元格被重复使用。左视图没有出现。此方法(initWithStyle:reuseIdentifier:)未加载。
[left setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];