3

在情节提要中,我设置了 tableviewcell 标识符“firstCell”和类“FirstTableViewCell”。
在 UIView 控制器中:

- (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];
}
4

1 回答 1

1

I would strongly suggest you to implement the method prepareForReuse in your FirstTableViewCell class. In that method clean the all elements that can't be reused by the cell (Example: UILabel text values). I don't suggest recreating UIViews in this method because the performance might be affected.

But it's difficult giving a complete judgment without looking at the whole code in the execution stack related for this problem.

Update Based on your code update, I suggest you to do the following

//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
 }

For this case calling:

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

Is overkill because you force to the system to redraw your views. Instead do your positioning in the layoutSubviews, which is called by your uiTableViewController everytime that the cell will appears in your table.

于 2013-09-12T13:24:37.577 回答