Iphone 选中UITableView单元格中的复选标记
我想在特定的TableView单元格中打勾 因此,我使用了以下代码:Iphone 选中UITableView单元格中的复选标记,iphone,uitableview,checkbox,checkmark,Iphone,Uitableview,Checkbox,Checkmark,我想在特定的TableView单元格中打勾 因此,我使用了以下代码: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { newRow = [indexPath row]; oldRow = [lastIndexPath row]; if (newRow != oldRow) { UITableViewCell *newCell = [tableVi
- (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设备上进行测试时,它会使应用程序崩溃
有什么解决办法吗
提前感谢。只需在“cellforrowatinexpath”方法中编写以下代码
[[cell imageView]setImage:[UIImage ImageName:@“tickmarkBlue.png”];
[[cell imageView]设置隐藏:是];
if()
{
[[cell imageView]设置隐藏:否];
}
最有可能导致崩溃的原因是ivar,lastIndexPath
。您将值存储在其中而不保留它们,因此它们可能随时被释放。尝试定义名为lastIndexPath
(手动引用计数为retain
,自动引用计数为strong
)的属性。然后可以使用self.lastIndexPath=indepath
或[self-setLastIndexPath:indepath]
另外,像这样强行更改单元格内容也是不好的。最好先存储选定的索引,然后重新加载表数据。为获得最大效率,仅更新更改的单元格。让您的
cellForRowAtIndexPath:
方法打开和关闭复选标记。崩溃错误消息是什么?对oldRow的EXE\u BAD\u访问=[lastIndexPath行];将更改应用于单元格中,如箭头所示:
是正确的。然而,这并不能直接解决原始海报的崩溃问题。另外,此代码是为[cell setAccessoryType:UITableViewCellAccessoryCheckmark]
不适合的任务编写的吗?谢谢。我已经按照你的建议用过了。我已经为lastIndexPath(NSIndexPath)创建了属性并对其进行了合成。。在模拟器中工作良好。但iPhone设备仍然存在问题。您是否充分使用了您的新设备?是否替换了oldRow=[lastIndexPath行]代码>与oldRow=[[self-lastIndexPath]行]
和lastIndexPath=indexath的两种情况代码>与[self-setLastIndexPath:indexPath]代码>?您是否可以编辑您的帖子以显示修改后的代码,cellForRowAtIndexPath:
和didSelectRowAtIndexPath:
?如果它成功地识别出导致崩溃的线路,那也很好知道。嘿,现在得到了没有错误的解决方案。我会在办理登机手续后告诉你。。无论如何,谢谢。
[[cell imageView] setImage:[UIImage imageNamed:@"tickmarkBlue.png"]];
[[cell imageView] setHidden:YES];
if(<your condition here>)
{
[[cell imageView] setHidden:NO];
}