문제

I need to allow user to select multiple choices for some items. For this purpose i'm using following.

Only problem is that, i have select row twice to remove the accessoryView. I can select the row on first selection. however after selecting row if touches it to deselect it, first only highlight color is going, then i have to click again to deselect that row.

Any idea why this is happening? I want to deselect the row on very first touch.

- (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];
}
}

EDIT :

- (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;
}
도움이 되었습니까?

해결책 2

As nobody answered this question, I have to do it myself.

I finally got solution for this kind of situation by useing [tableView reloadData] .

I can add a advice for all ios users, ALways reload table after performing any changes to Data of table.

다른 팁

If you will use [tableView reloadData] the whole tableView will be reloaded.

It won't take much memory & execution time on simulator but on real iPhone / iPad / iPod it'll take more resources.

Replace [tableView reloadData] with [tableView reloadRowsAtIndexPaths: [NSArray arrayWithObjects: [NSIndexPath indexPathForRow: indexPath.row inSection: indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];

You can replace UITableViewRowAnimationNone with

UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top