我已经在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/