简体   繁体   中英

How to change tableview cell image?

I have table view in side bar which is similar to facebook app. In my sidebar tableview i have imageview in each row. I want the imageview will be changed when i clicked the cell and it will be retain while for selecting another cell. Here is my code

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];

[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

 #pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(@"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);

[UIView commitAnimations];
if (self.sidebarDelegate) {
    NSObject *object = [NSString stringWithFormat:@"ViewController%d", indexPath.row];
    [self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
    [delegate.firstLensePageObj timerFunction:indexPath.row];
}

}

Try this in your didSelectRowAtIndexPath delegate method :-

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"yourImage"];

Try this in cellForRowAtIndexPath.

  UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Your Image"]];

  cell.selectedBackgroundView = selectedImageView;

Need to capture the selected indexpath in didSelectRowAtIndexPath: method and need to reload the tableview in that method. In cellForRowAtIndexPath need to check the selected index with current index.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

    UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
    [cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
    [cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
    [contentView addSubview:cellImg];

    [contentView setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:contentView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    }

     #pragma mark - Table view delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    delegate.sideBarSelectedIndex = indexPath.row;
    NSLog(@"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);

    [UIView commitAnimations];
    if (self.sidebarDelegate) {
        NSObject *object = [NSString stringWithFormat:@"ViewController%d", indexPath.row];
        [self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
        [delegate.firstLensePageObj timerFunction:indexPath.row];
    }

    }

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