2013-03-11 79 views
10

我想显示在使用此代码我UITableViewCells的具体行的“挂锁”图标:的UITableView细胞accessoryView的形象问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
      cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
      [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 

    } 

    return cell; 
} 

我得到一个有趣的结果 - 挂锁最初显示的行5 ,但是当我向下滚动列表时,图标在其他单元的accessoryView上随机重新显示(只有1个部分btw),并且滚动变得相当生涩和迟缓...我向上/向下滚动它显示的实例越多! 为什么?这里的错误在哪里?

帮助,谢谢!

+0

因为当你滚动单元已经重复使用其他行,我认为你应该使用不同的CellReuseIdentifier 5行和9 – 2013-03-11 22:56:22

+0

我该怎么做呢? – mindbomb 2013-03-11 23:05:30

回答

21

单元格被重用。您需要每次重置accessoryView。 它只是一个小的变化:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]]; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 
    } else { 
     cell.accessoryView = nil; 
    } 

    return cell; 
} 

刚刚完成,你也可以使用Eric的解决方案这样的 - 它可能会更快,因为ImageView的不创建每次。但那只是一个很小的差别。可能你的滞后有其他原因。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 
    if(indexPath.row == 5 || indexPath.row == 9) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"]; 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 
    } 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    return cell; 
} 
+0

好的,谢谢!我会在稍后检查。但实际上我需要没有图像的单元显示'披露'灰色直角图标 – mindbomb 2013-03-12 12:17:54

+0

是5和9仍然会显示图像。其他人不是。去尝试一下。 – calimarkus 2013-03-12 13:32:20

+0

并阅读uitableview文档 - 它解释了重用。 – calimarkus 2013-03-12 13:32:47

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    if(indexPath.row == 5 || indexPath.row == 9) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCellWithImage"]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell"]; 
    } 

    GPBTopic *topic = [self.topics.list objectAtIndex:indexPath.row]; 
    cell.textLabel.text= topic.name; 

    if ((indexPath.row == 5) || (indexPath.row == 9)) 
    { 
     cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"lock_icon.png"]];; 
     [cell.accessoryView setFrame:CGRectMake(0, 0, 24, 24)]; 

    } 

    return cell; 
} 
+0

对不起,不正确,jaydee3的解决方案更简单..谢谢! – mindbomb 2013-03-13 01:00:11