我需要允许用户为某些项目选择多个选项。为此,我正在使用以下内容。
唯一的问题是,我有两次选择行来删除附件视图。我可以在第一次选择时选择行。但是在选择行后,如果触摸它以取消选择它,首先只有突出显示颜色,然后我必须再次单击以取消选择该行。
知道为什么会这样吗?我想在第一次触摸时取消选择该行。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int i=indexPath.row;
if(self.multi == 1){
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"accessoryType:%d",selectedCell.accessoryType);
if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *cellText = selectedCell.textLabel.text;
[self.selectedValues replaceObjectAtIndex:i withObject:cellText];
}else{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedValues replaceObjectAtIndex:i withObject:@""];
}
}
else{
NSLog(@"not for multi select",nil);
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
[self.delegate addItemViewController:self setPeculiarity:cellText setIndex:self.index];
[self dismissModalViewControllerAnimated:YES];
}
}
编辑 :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [pecs objectAtIndex:indexPath.row];
if(self.selectedValues.count > 0){
for(int k = 0 ; k < [selectedValues count];k++){
if ([[self.selectedValues objectAtIndex:k] isEqualToString:[pecs objectAtIndex:indexPath.row]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"pecs:%@",[pecs objectAtIndex:indexPath.row]);
}
}
}
// for background color
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.2823 green:0.4509 blue:0.8588 alpha:1.0];
[cell setSelectedBackgroundView:bgColorView];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
return cell;
}