xcode 使用 UITableViewCellAccessoryCheckMark
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13214471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using UITableViewCellAccessoryCheckMark
提问by Zack
I am trying to make this work but can't seem to wrap my mind around the idea.
我正在努力完成这项工作,但似乎无法理解这个想法。
So far I have it to where you are able to select a cell and it be able to produce a checkmark. Then, when you select a different cell, the previous checkmark would go away and the tableview would make a new checkmark on the new cell you just selected. This all works beautifully.
到目前为止,我已经有了它,你可以选择一个单元格,它可以产生一个复选标记。然后,当你选择一个不同的单元格时,之前的复选标记会消失,tableview 会在你刚刚选择的新单元格上做一个新的复选标记。这一切都很好。
However, I am wanting to make it to where when you select the same cell with the checkmark, the checkmark does not disappear. But, it does!
但是,当您选择带有复选标记的同一单元格时,我想将其设为该复选标记不会消失的位置。但是,确实如此!
I have tried numerous amounts of if statements to see if I can figure out how to make this work but can't figure out a solution. It is probably something minor that I need to rearrange in my code.
我已经尝试了大量的 if 语句,以查看我是否能弄清楚如何进行这项工作,但无法找到解决方案。这可能是我需要在代码中重新排列的小事。
Here is my code:
这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
cellCheck = [tableView cellForRowAtIndexPath:indexPath];
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"%@", indexPath);
}
}
回答by Vito Limandibhrata
If you select the cell with checkmark, this code will be executed.
如果您选择带有复选标记的单元格,则将执行此代码。
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
This code will remove the checkmark which you have added during the first select.
此代码将删除您在第一次选择时添加的复选标记。
then this code will be executed
那么这段代码将被执行
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
Thus only when you select the same cell again, the checkmark will reappear
因此只有当您再次选择相同的单元格时,复选标记才会重新出现
I think the cleaner way will be as followed.
我认为更清洁的方式如下。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cellCheck = [tableView
cellForRowAtIndexPath:indexPath];
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:indexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
AS you can get the value for self.checkedIndexPath from [tableView indexPathForSelectedRow]; the setting of self.checkedIndexPath is optional depending on the logic of your code.
因为您可以从 [tableView indexPathForSelectedRow] 中获取 self.checkedIndexPath 的值;self.checkedIndexPath 的设置是可选的,具体取决于您的代码逻辑。
回答by oskob
You don't manipulate the cells directly like that, store the checked state somewhere and change the accessoryType in cellForRowAtIndex:
您不会像那样直接操作单元格,将选中的状态存储在某处并更改 cellForRowAtIndex 中的附件类型:
static BOOL checkedRows[100]; // example data storage
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
checkedRows[indexPath.row] = !checkedRows[indexPath.row];
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"somecell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.accessoryType = checkedRows[indexPath.row] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}