简体   繁体   中英

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM