我在 UITableView 中有 2 个部分。我想在我选择的行上启用 UITableViewCellAccessoryCheckmark。
2 个部分中只能有 1 个选中的行。
我怎样才能做到这一点?我发现的示例仅显示了如何使用 1 部分进行操作。
谢谢你。
问问题
652 次
4 回答
1
这将适用于每个部分中的任意数量的部分和单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section]].accessoryType = UITableViewCellAccessoryCheckmark;
[self deSelectOtherCellsInTableView:tableView Except:indexPath];
}
-(void)deSelectOtherCellsInTableView:(UITableView *)tableView Except:(NSIndexPath *)indexPath
{
for(UITableViewCell *cell in [tableView visibleCells]){
NSIndexPath *index = [tableView indexPathForCell:cell];
if(index.section == indexPath.section && index.row != indexPath.row){
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
于 2013-08-01T19:12:49.347 回答
0
您可以保留一个存储节号和行号的变量,以及一个布尔值来跟踪是否已进行选择。在该didSelectRow
方法中,您可以访问这些值以从前一个单元格中删除选择(如果有),并将它们更新为当前选定的行和节值。
于 2012-02-15T07:31:57.880 回答
0
这可能是您可以提供帮助的。在 UITavleview 的委托方法上,您可以实现此代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section==1)
{
NSUInteger row=indexPath.row;
// implement you code for first section;
}
elseif(indexPath.section==2)
{
NSUInteger row=indexPath.row;
//implement you code second first section;
}
.....................
.....................
.....................
}
于 2012-02-15T08:40:47.877 回答
0
这里我假设每个部分的总行数为 3,部分的总数为 2。
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for(NSInteger i=0;i<2;i++)
{
for (int index=0; index<3; index++)
{
NSIndexPath *indexpath=[NSIndexPath indexPathForRow:index inSection:i];
UITableViewCell *cell=[tableView1 cellForRowAtIndexPath:indexpath];
if ([indexPath compare:indexpath] == NSOrderedSame)
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType=UITableViewCellAccessoryNone;
}
}
}
}
于 2012-02-15T09:59:56.577 回答