【发布时间】:2013-05-14 07:08:43
【问题描述】:
花了好几个小时都没有用。我不明白我是否在搜索(谷歌搜索)方面没有效率,还是这方面的问题较少,或者我在执行专家的答案时可能犯了一些错误!
我知道有几个关于在一节中为一行设置附件类型复选标记而为其他行设置不复选标记的问题,追踪帖子here 和there。
我的表格视图中有 2 个部分。默认情况下,我希望选择每个部分中的第一行,即带有辅助视图复选标记。现在,在用户选择一行后,我希望复选标记可见仅在选定的行上。我尝试声明两个索引路径来跟踪每个部分中的行选择。这是我的实现代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (indexPath.section == 0)
{
if(self.firstSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (indexPath.section == 1)
{
if(self.secondSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
switch (indexPath.section)
{
case 0:
{
if (indexPath.row == 0)
{
if(self.firstSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = @"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"No";
}
}
break;
case 1:
{
if (indexPath.row == 0)
{
if(self.secondSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = @"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = @"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = @"Hard";
}
}
break;
default:
break;
}
}
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
return cell;
}
在我的 didSelectRowAtIndexPath 中,我做了以下操作:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
self.firstSectionIndex = indexPath;
}
if (indexPath.section == 1)
{
self.secondSectionIndex = indexPath;
}
[tableView reloadData];
}
我搜索了要保存的单元格选择状态以供长期参考,在寻找它时,我找到了一些有用的链接here。
但是它正在选择多个单元格,并且正在为部分中的所有行应用附件类型复选标记。我不明白出了什么问题。谁能指导我!
提前谢谢大家:)
【问题讨论】:
-
谷歌搜索而不是谷歌搜索:)
-
我注意到您从未清除过
self.firstSectionIndex或self.secondSectionIndex,这不可能? -
@LithuT.V 已编辑 :),你能帮帮我吗
-
@trojanfoe 你的意思是说设置 firstSectionIndex 和 secondSectionIndex 为零??
-
是的;在设置一个的代码中,您不需要取消设置另一个吗?
标签: iphone savestate uitableview checkmark