IOS:UITableView必须双击才能取消选择
我有一个UITableView,其中包含一个tableView,它从可变数组中填充“原型单元格”,当您选择这些单元格时,这些单元格会显示一个“附件复选标记”。我在tableview下面的视图中有一个textfield,然后将其数据附加到填充tableview的数组中。我的问题是,在我将新数据添加到tableview后,我必须触摸一个单元格两次,才能取消选择我以前选择的任何单元格IOS:UITableView必须双击才能取消选择,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有一个UITableView,其中包含一个tableView,它从可变数组中填充“原型单元格”,当您选择这些单元格时,这些单元格会显示一个“附件复选标记”。我在tableview下面的视图中有一个textfield,然后将其数据附加到填充tableview的数组中。我的问题是,在我将新数据添加到tableview后,我必须触摸一个单元格两次,才能取消选择我以前选择的任何单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cel
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"skillName" forIndexPath:indexPath];
cell.textLabel.text = skillsOptions[indexPath.row];
// Configure the cell...
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = YES;
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[skillsList removeObject:skillsOptions[indexPath.row]];
}
-(void)grabSelectedSkills {
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
NSString *skill = skillsOptions[indexPath.row];
//NSString *skill = [NSString stringWithFormat:@"%li",(long)indexPath.row];
[skillsList addObject:skill];
}
NSLog(@"skillsList: %@",skillsList);
}
- (IBAction)continue:(id)sender {
[self grabSelectedSkills];
}
- (IBAction)addOtherSkills:(id)sender {
if (self.otherSkill.text.length > 1) {
[skillsOptions addObject:self.otherSkill.text];
self.otherSkill.text = @"";
[self.tableView endEditing:YES];
[self.tableView reloadData];
}
我认为您应该只使用didSelectRowAtIndexPath:method删除didSelectRowAtIndexPath:method并选中该单元格(如果不是),反之亦然-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL cellSelected = NO;
// set cellSelected based on your condition
if(cellSelected)
{
// code to deselect the cell
}
else
{
// code to select the cell
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
您需要在单元格外跟踪您的选择/取消选择状态-由技能选项键入的字典可能是一个不错的选择 然后,您可以在CellForRowatineXpath和DidSelectRowatineXpath中使用此选项,根据需要添加/删除复选标记。您应该取消选择didSelectRowAtIndexPath中的行,并删除didSelectRowAtIndexPath- 创建self.selectedCells作为NSMutableDictionary属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"skillName" forIndexPath:indexPath];
// Configure the cell...
NSString *skillOption=skillsOptions[indexPath.row];
cell.textLabel.text = skillOption;
if (self.selectedCells[skillOption]!= nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *skillOption=skillsOptions[indexPath.row];
if (self.selectedCells[skillOption]!= nil) {
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedCells removeObjectForKey:skillOption];
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedCells[skillOption]=skillOption;
}
[tableview deselectRowAtIndexPath:indexPath];
}
您需要在单元格外跟踪您的选择/取消选择状态-由技能选项键入的字典可能是一个不错的选择。然后,您可以在CellForRowatineXpath和DidSelectRowatineXpath中使用此选项,根据需要添加/删除复选标记。您应该取消选择didSelectRowAtIndexPath中的行并删除didSelectRowAtIndexPath所选单元格的作用是什么?我不知道怎么叫它。我必须将其绑定到某个对象吗?cellSelected只是一个BOOL变量,用于保存单元格是否已选中的状态,每次触摸单元格时,选中/未选中DidSelecterOwatineXPath:将被调用您无需绑定任何对象,只需删除DecellRowatindexpath:并在基于cellSelected BOOL状态的didSelectRowAtIndexPath:中执行取消选择操作感谢您的帮助@sanjay