2013-09-12 8 views
3

в раскадровке Я установил tableviewcell Идентификатор «firstCell» и класс «FirstTableViewCell».
В UIViewController:Почему customTableViewCell отличается и медленнее?

- (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

в 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]; 
[email protected]"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

Имеет ли ваш файл 'FirstTableViewCell' XIB-файл? – Amar

+0

Вставьте свой код из initWithStyle из этой пользовательской ячейки - что вы там делаете? Из вашего вывода видно, что первые два вызова используют ячейку (тот же адрес памяти 0x75624b0), а третий создает чистый. Вы также подготовилиForReuse, реализованный в вашем классе? –

+0

@GrzegorzKrukowski Вставьте часть кода ячейки. У меня нет готовой команды. Я положил 2 UILabel и раскладушку с использованием uiview. – wmmj23

ответ

1

Я настоятельно рекомендую вам реализовать метод prepareForReuse в классе FirstTableViewCell. В этом методе очистите все элементы, которые нельзя повторно использовать сотой (пример: значения текста UILabel). Я не предлагаю воссоздавать UIViews в этом методе, потому что производительность может быть быть затронутой.

Но сложно дать полное суждение, не глядя на весь код в стеке выполнения, связанный с этой проблемой.

Update На основе вашего обновления кода, я предлагаю вам сделать следующий

//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 каждый раз, когда ячейка появится в вашей таблице.

+0

Это не работает. – wmmj23

+0

можете ли вы пройти весь код в своем классе? – rodchile

+0

Я положил весь код. – wmmj23

Смежные вопросы