【问题标题】:How to change tableview cell image?如何更改表格视图单元格图像?
【发布时间】:2013-10-31 05:41:49
【问题描述】:

我在侧边栏中有表格视图,类似于 facebook 应用程序。在我的侧边栏表格视图中,我在每一行都有图像视图。我希望单击单元格时会更改图像视图,并且在选择另一个单元格时会保留该图像视图。这是我的代码

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

}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    在你的 didSelectRowAtIndexPath 委托方法中试试这个:-

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

    【讨论】:

      【解决方案2】:

      在 cellForRowAtIndexPath 中试试这个。

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

      【讨论】:

        【解决方案3】:

        需要在didSelectRowAtIndexPath:方法中捕获选中的indexpath,并且需要在该方法中重新加载tableview。在cellForRowAtIndexPath中需要检查选中的索引和当前索引。

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

        【讨论】: