ユーザーがいくつかのアイテムに対して複数の選択肢を選択できるようにする必要があります。この目的のために、私は以下を使用しています。
唯一の問題は、accessoryView を削除するために行を 2 回選択したことです。最初の選択で行を選択できます。ただし、行を選択した後、タッチして選択を解除すると、最初にハイライト色のみが表示され、もう一度クリックしてその行の選択を解除する必要があります。
なぜこれが起こっているのですか?最初のタッチで行の選択を解除したい。
- (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;
}