0

我想要 Checkmark 特别是 TableView Cell。

所以我使用了代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

newRow = [indexPath row];
oldRow = [lastIndexPath row];

if (newRow != oldRow) {
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone;

    lastIndexPath = indexPath; 
}
else{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    lastIndexPath = indexPath;
}}

它在 iPhone 模拟器中工作正常。但是在 iPhone 设备中进行测试时,它会使应用程序崩溃。

对此有什么解决方案..?

提前致谢。

4

2 回答 2

2

您崩溃的最可能原因是您的 ivar lastIndexPath,. 您将值存储在其中而不保留它们,因此它们可能随时被释放。尝试定义一个名为lastIndexPath(retain用于手动引用计数,strong用于自动引用计数) 的属性。然后你可以使用self.lastIndexPath = indexPathor [self setLastIndexPath:indexPath]

此外,像这样强行更改单元格内容也很糟糕。最好先存储选中的索引,然后重新加载表数据。为了获得最大效率,仅更新更改的单元格。让您的cellForRowAtIndexPath:方法打开和关闭复选标记。

于 2012-04-25T04:35:43.273 回答
0

只需在“cellForRowAtIndexPath”方法中编写以下代码。

[[cell imageView] setImage:[UIImage imageNamed:@"tickmarkBlue.png"]];
[[cell imageView] setHidden:YES]; 

if(<your condition here>)
{
      [[cell imageView] setHidden:NO];
}
于 2012-04-25T04:32:19.423 回答