ios - 为什么 customTableViewCell 不同且速度慢?

标签 ios uitableview

在 Storyboard 中,我设置了 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>>

为什么不一样?你可以看到第二个单元格和第一个单元格之间的时间比第三个单元格和第二个单元格之间的时间要大,为什么?谢谢。

@implementation FirstTableViewCell

在单元格中:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
}
return self;
}
-(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 *left =(UIView *) [self.contentView viewWithTag:3];
UIView *fill;



int tmp = [ca.id intValue];

switch (tmp) {
    case 1:
        fill = [[CreditView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];         

        break;
    case 2:
        fill = [[BankView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
        break;
    case 3:
        fill = [[QuestionView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
        break;
    case 4:
        fill = [[SoftwareView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
        break;
    case 5:
        fill = [[NoteView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
        break;

    default:
        break;
}

[left addSubview:fill];
[left setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];

}

-(void)setcaNil:(int) sum {


UILabel *nameLabelView = (UILabel *)[self.contentView viewWithTag:1];
nameLabelView.text=@"All";

UILabel *detailLabelView = (UILabel *)[self.contentView viewWithTag:2];
detailLabelView.text=[[NSNumber numberWithInt:sum] stringValue];

UIView  *f =(UIView *) [self.contentView viewWithTag:3];

AllView * ui = [[AllView alloc]initWithFrame:CGRectMake(0, 0, 27, 27)];
[f addSubview:ui];


[f setNeedsDisplay];
[nameLabelView setNeedsDisplay];
[detailLabelView setNeedsDisplay];
}

最佳答案

我强烈建议您在 FirstTableViewCell 类中实现 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];

是矫枉过正,因为您强制系统重绘您的 View 。取而代之的是在 layoutSubviews 中进行定位,每次单元格出现在表格中时,uiTableViewController 都会调用它。

关于ios - 为什么 customTableViewCell 不同且速度慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764962/

相关文章:

ios - 在 UIViewController 之间切换时会发生什么类型的内存问题?

ios - UITableViewDataSource 无法附加到类,swift-ios 中编译错误

ios - 为标识符注册的无效 Nib

ios - 如何处理循环中的json错误或停止它

ios - 当选择 UITableViewCell 时,UIImageView 会更改其位置

ios - 如何从 AppDelegate 调用不同类的实例方法?

ios - 授予 Jenkins 解锁钥匙串(keychain)的权限

ios - 如何滚动到 UITableViewCell 的底部而不是 TextField

ios - TableView didSelectRowAtIndexPath,仅在选择第二次单击后有效

ios - 容器 View 的问题