Ios 选择一个UITableViewCell将在滚动后选择另一个单元格
虽然这是一个常见的问题,但我不知道如何解决这个问题 我提到了,但我不明白 我有allMumbaiArray(在cellForRowAtIndexPath中使用)和tableviewalllMumbaiTableview。通过使用以下代码,我可以选择和取消选择该行 根据EI队长的解决方案更新Ios 选择一个UITableViewCell将在滚动后选择另一个单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,虽然这是一个常见的问题,但我不知道如何解决这个问题 我提到了,但我不明白 我有allMumbaiArray(在cellForRowAtIndexPath中使用)和tableviewalllMumbaiTableview。通过使用以下代码,我可以选择和取消选择该行 根据EI队长的解决方案更新 @property (nonatomic, strong) NSMutableArray *arrIndexpaths; tableView方法-: - (UITableViewCell *)tableV
@property (nonatomic, strong) NSMutableArray *arrIndexpaths;
tableView方法-:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
}
cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row];
if ([self.arrIndexpaths containsObject:[NSNumber numberWithInt:indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view endEditing:YES];
NSString *testing =[allMumbaiArray objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(@"%@",testing);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSNumber numberWithInt:indexPath.row]];
}
问题:1)当我在滚动表格后选择第一行时,查看另一行是否被选中。如何解决此问题
2) 连续选择和取消选择行后,如何维护仅选择的行值?以下是解决方案
创建一个可变数组,用于存储tableview单元格的选定索引
@property (nonatomic, strong) NSMutableArray *arrIndexpaths;
cellForRowAtIndexPath
if ([self.arrIndexpaths containsObject:[NSNumber numberWithLong: indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
didSelectRowAtIndexPath
if ([self.arrIndexpaths containsObject:[NSNumber numberWithLong: indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
DID取消行索引路径
if ([self.arrIndexpaths containsObject:[NSNumber numberWithLong: indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
这里是示例项目
如果您的手机没有显示附件,则只需重新加载即可
[_ableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
在
didSelectRowAtIndexPath
和didSelectRowAtIndexPath
中尝试此操作 存储选定单元格的indexpath,并根据CellForRowatineXpath
Method中的fill tableview启用多重选择吗?@Misha是,但在滚动时(我遵循EL Captain的解决方案,但仍然面临问题->即实际选中单元格,但未显示UITableViewCellAccessory复选标记。)在cellForRowAtIndexPath中,滚动其进入else{cell.accessoryType=UITableViewCellAccessoryNone;}因此,UITableViewCellAccessoryCheckmark再次在滚动后消失。NSIndexSet比数组更简单、更高效NSNumbers@EICaptain根据您的解决方案更新了我的问题。@EICaptain ok等待…:)@艾卡普坦,但你的答案还不起作用?我将检查演示项目:(