- (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>>
为什么它与众不同?你可以看到第二个细胞和第一个细胞之间的时间大于第三个细胞和第二个细胞之间的时间,为什么? THX。
@implementation FirstTableViewCell
在Cell:
- (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];
}
答案 0 :(得分:1)
我强烈建议您在FirstTableViewCell类中实现prepareForReuse方法。在该方法中,清除单元格无法重用的所有元素(例如:UILabel文本值)。我不建议在此方法中重新创建UIViews,因为性能 可能会 受到影响。
但是如果不查看与此问题相关的执行堆栈中的整个代码,就很难做出完整的判断。
<强>更新强> 根据您的代码更新,我建议您执行以下操作
//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];
过度杀戮是因为你强迫系统重绘你的视图。而是在layoutSubviews中进行定位,每次单元格出现在表格中时,uiTableViewController都会调用它。