在 storyboard 里设置了 UITableViewCell Identifier "firstCell" 和 class "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 22:26:08.126 Info[5166:c07] <FirstTableViewCell: 0x7579180; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x7579300>>
2013-09-12 22:26:08.342 Info[5166:c07] <FirstTableViewCell: 0x7579180; baseClass = UITableViewCell; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7579300>>
2013-09-12 22:26:08.344 Info[5166:c07] <FirstTableViewCell: 0x819b210; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x8179ad0>>
2013-09-12 22:26:08.346 Info[5166:c07] <FirstTableViewCell: 0x8378360; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x8375df0>>
可以看到第二行和第一行的内存地址是一个,但是第二行出现的慢,第二行以后地址都在变,响应速度也快了这是出于什么原因呢?代码功能就是在不同的行设置不同的label 和画个不同的uiview
Cell代码: @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];
}