2011-07-06 75 views

回答

0

如果你想重用细胞,然后在的cellForRowAtIndexPath代码一样,

static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) 
    { 
    CGRect CellFrame; 

     CGRect nameLabelFrame; 

     CGRect countLabelFrame; 

     CellFrame = CGRectMake(0, 0, 320, 80); 

     nameLabelFrame = CGRectMake(10, 10, 270, 50); 
     countLabelFrame = CGRectMake(10, 58, 270, 12); 


     cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease]; 
     UILabel *lblTemp; 
     //UIImageView *imgTemp; 

     lblTemp = [[UILabel alloc] initWithFrame:nameLabelFrame]; 
     lblTemp.tag = 1; 
     [cell.contentView addSubview:lblTemp]; 
     [lblTemp release]; 



     lblTemp = [[UILabel alloc] initWithFrame:countLabelFrame]; 
     lblTemp.tag = 3; 
     [cell.contentView addSubview:lblTemp]; 
     [lblTemp release]; 

} 

//And outside of if condition access these labels by this 

    //UIConstruction 
     UILabel *nameLabel = (UILabel *)[cell viewWithTag:1]; 

     UILabel *countLabel = (UILabel *)[cell viewWithTag:3]; 

一些事情,并使用这些标签或控制你想要超出这个东西如果条件

+0

我不知道为什么这是downvoted ??? !!!! – KingofBliss

+0

'initWithFrame:reuseIdentifier'在3.0中已被弃用,现在使用委托方法设置高度。另外,代替使用标签,对UITableViewCell进行子类化并将UILabels设置为该类的属性会更好,特别是因为当通过覆盖'setFrame来调整单元格大小时可以调整它们的大小:' – EmilioPelaez

+0

@EmilioPelaez通过设置框架这并不意味着,你不会使用heightForRowAtIndexPath。这是为了调整frame.also我没有提供确切的解决方案。只是一个想法,没有人会写整个code.also还有另一种方法重用cell.so解决方案是重用该单元格。 – Ishu