ios - 在iOS的UITableView中获取选中标记行

标签 ios uitableview cell

我已经在cellForRowAtIndexPath上以编程方式检查了一些行,并且显示并正常工作。但是现在我需要获取选中标记的行。我使用以下代码:

for (int i=0; i<10; i++) {

    NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];

    if([motorwayTable cellForRowAtIndexPath:index].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        NSLog(@"Selected Rows: %d",i);
    }
}

这是我的cellForRowAtIndexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


cell.textLabel.text = [NSString stringWithFormat:@"arefin %d",[indexPath row]];

if(indexPath.row == 8 || indexPath.row == 7 || indexPath.row == 9)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.selected = YES;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;

}

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

return cell;

}

但是问题是我只获得可见的选中标记行,而没有得到不可见的行。如何获取在UITableView中尚不可见但已在cellForRowAtIndexPath中以编程方式检查的选中标记的行。

提前致谢!

最佳答案

之所以只得到可见的行,是因为在屏幕外滚动单元时会释放它们。建议您替代-tableView:didSelectRowAtIndexPath:方法,而不是当前的解决方案。在此方法中,测试传递到该方法中的索引路径处的单元格是否具有选中标记,如果是,则将索引路径相应地存储在NSMutableSet中。

例:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) {
            [_selectedCellIndexes addObject:indexPath];
        }
}

关于ios - 在iOS的UITableView中获取选中标记行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500162/

相关文章:

ios - swift 3 WKWebView委托(delegate)WKNavigationDelegate不明确的方法

ios - iOS-tableView,按降序对JSON数组排序

swift - NSTableView,使它对用户友好

jQuery 隐藏表格单元格中的子字符串

ios - 是否可以部分执行 ARWorldMap 重新定位而不是立即添加所有 ARAnchors?

objective-c - 强制方向改变

ios - 如何在 Swift 中识别 UIImage 数组中的 Tapped UIImage

ios - 将 Searchbar 添加到 NavigationBar 但 TableViewCells 未加载信息

ios - UITextField 阻止 UITableViewCell 内的滚动

swift - cell.imageView!.frame 尺寸变化。 swift