Sravan Kumar
Sravan Kumar

Reputation: 94

Change Imageview image in tableview cell at selected index

If I select another cell image stays same for the previous cell, please help me out.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    VoteDeatailTableViewCell *cell = [_voteTable dequeueReusableCellWithIdentifier:@"VoteDeatailTableViewCell"];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.imgRadio.image  = [UIImage imageNamed:@"radio_uncheck"];
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    VoteDeatailTableViewCell *cell = [_voteTable cellForRowAtIndexPath:indexPath];
    cell.imgRadio.image  = [UIImage imageNamed:@"radio_check"];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

Upvotes: 0

Views: 412

Answers (1)

Kuldeep
Kuldeep

Reputation: 4552

If you change your cell imageView in didSelectRowAtIndexPath so when you scroll your tableView it mismatched.

so my suggestion is that add selected indexPath in NSMutableArray and in cellForRowAtIndexPath check that array contains selected indexPath if it contains than set radio_check image otherwise set radio_uncheck image like below. Define NSMutableArray globally.

NSMutableArray *arrSelectedImages = [[NSMutableArray alloc] init];


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VoteDeatailTableViewCell *cell = [_voteTable dequeueReusableCellWithIdentifier:@"VoteDeatailTableViewCell"];
    cell.contentView.backgroundColor = [UIColor clearColor];

    if ([arrSelectedImages containsObject:indexPath]) {
        cell.imgRadio.image  = [UIImage imageNamed:@"radio_check"];
    }
    else {
        cell.imgRadio.image  = [UIImage imageNamed:@"radio_uncheck"];
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [arrSelectedImages removeAllObjects];
    [arrSelectedImages addObject:indexPath];
    [self.tblVW reloadData];
}

Upvotes: 1

Related Questions