Ios 如何更改tableview单元格图像?

Ios 如何更改tableview单元格图像?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在侧栏中有类似于facebook应用程序的表格视图。在我的侧栏表格视图中,每一行都有imageview。我希望在单击单元格时imageview将被更改,并且在选择其他单元格时它将被保留。这是我的密码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

我在侧栏中有类似于facebook应用程序的表格视图。在我的侧栏表格视图中,每一行都有imageview。我希望在单击单元格时imageview将被更改,并且在选择其他单元格时它将被保留。这是我的密码

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

}

在您的
didSelectRowAtIndexPath
delegate方法中尝试此操作:-

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

在CellForRowatineXpath中尝试此选项

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

  cell.selectedBackgroundView = selectedImageView;

需要在didSelectRowAtIndexPath:方法中捕获选定的indexpath,并需要在该方法中重新加载tableview。在单元格中,需要使用当前索引检查所选索引

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

    }