如何观察UITableViewCellAccessoryCheckmark是否检查了电池附件类型?
在我的应用程序中,我添加UITableView了一个UIAlertView. 我也用过
cell.accessoryType = UITableViewCellAccessoryCheckmark;
在我的 tableView 中,我有 9 行。当用户点击第一行时,所有行都会被选中,除了第二行。用户可以根据需要检查任何行/行。当用户点击第二行时,只检查第二行。
提前致谢 :)
如何观察UITableViewCellAccessoryCheckmark是否检查了电池附件类型?
在我的应用程序中,我添加UITableView了一个UIAlertView. 我也用过
cell.accessoryType = UITableViewCellAccessoryCheckmark;
在我的 tableView 中,我有 9 行。当用户点击第一行时,所有行都会被选中,除了第二行。用户可以根据需要检查任何行/行。当用户点击第二行时,只检查第二行。
提前致谢 :)
由于项目的数量非常少,您可以使用单个整数和位掩码对选中/未选中状态进行建模。使用数字的低九位表示选中/未选中状态:
// Add an instance variable to your view controller
// Initialize to 0 in the designated initializer
NSUInteger checkedFlags;
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *res = ... // Dequeue the cell and so on
    res.accessoryType = (checkedFlags & (1 << indexPath.row))
    ?    UITableViewCellAccessoryCheckmark
    :    UITableViewCellAccessoryNone;
    ... // The rest of the method
}
以下是您选中/取消选中/切换单个行的方法:在tableView:willSelectRowAtIndexPath:您的委托中,执行以下操作之一:
checkedFlags |= (1 << indexPath.row); // Check
checkedFlags &= ~(1 << indexPath.row); // Uncheck
checkedFlags ^= (1 << indexPath.row); // Toggle
如果您想一次检查/取消选中多行,请使用一个数字进行掩码,该数字由对应于二进制形式的正在检查/取消检查的行的位组成。例如,设置了位 2..8 的数字是0x01FC0。
checkedFlags |= 0x1FC0; // Check rows #3 through #9
reloadData操作checkedFlags变量后不要忘记调用。
虽然@dasblinkenlight 的答案肯定更优雅,但对您来说可能太高级了。
本质是跟踪哪些单元格被选中,哪些未被选中。每当发生变化时,重新加载表格视图以更新复选标记。
您需要创建自己的变量来跟踪它。位图非常经济且优雅,但可能难以理解并且无法扩展到大量行。或者,您可以使用数组。
NSMutableArray *checkedArray = [NSMutableArray arrayWithObjects:
                   [NSNumber numberWithBool:YES],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   [NSNumber numberWithBool:NO],
                   nil];
要更改给定索引路径的行:
[checkedArray replaceObjectAtIndex:indexPath.row 
                        withObject:[NSNumber numberWithBool:YES]]; //add
[checkedArray replaceObjectAtIndex:indexPath.row 
                        withObject:[NSNumber numberWithBool:NO]]; //remove
并cellForRowAtIndexPath确保您明确设置附件:
if ([[checkedArray objectAtIndex:indexPath.row] boolValue]) {
    cell.accessoryType = UITableViewCellAccessoryTypeCheckmark;
} 
else {
    cell.accessoryType = UITableViewCellAccessoryTypeNone;
}
    if ([[tableView cellForRowAtIndexPath:indexPath ] accessoryType] == UITableViewCellAccessoryCheckmark){
}
else{ 
}
    我知道这已经得到了回答,但是我很幸运地维护了一个 NSMutableIndexSet 实例,并为已检查/未检查的行添加/删除了索引。
关键方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSInteger index = indexPath.row;
    cell.accessoryType = ([self.selectedRowsIndexSet containsIndex:index]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d",index];
    return cell;
}
这是相当直截了当的 - 检查是否选择了索引并相应地设置附件。
对于多选(即可以选择任何调用)的实现,tableView:didSelectRowAtIndexPath:如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = indexPath.row;
    if ([self.selectedRowsIndexSet containsIndex:index])
        [self.selectedRowsIndexSet removeIndex:index];
    else
        [self.selectedRowsIndexSet addIndex:index];
    [tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
}
只需添加索引,重新加载表。对于单个选择,它也很快
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger oldIndex = [self.selectedRowsIndexSet firstIndex];
    NSInteger newIndex = indexPath.row;
    [self.selectedRowsIndexSet removeIndex:oldIndex];
    [self.selectedRowsIndexSet addIndex:newIndex];
    [tableView reloadRowsAtIndexPaths:@[ indexPath, [NSIndexPath indexPathForRow:oldIndex inSection:0] ]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
}
当然可以通过更多部分变得更复杂,某些部分中的选择是否依赖于其他部分中的选择,等等。我一直在为前者使用索引集的每节数组,然后为后者使用涉及底层数据模型的各种复杂逻辑。但是将基本的表视图交互保持在仅查询索引集的级别有助于使这些方法非常简短和可读。