简体   繁体   English

选择单元格后如何更改UITableViewCell图像?

[英]How to change a UITableViewCell image when the cell is selected?

The question sums it up-- I'm trying to have a box get checked when someone clicks on the UITableViewCell, which involves changing the image in the cell. 问题总结起来-我试图让有人单击UITableViewCell时选中一个框,这涉及更改单元格中的图像。 The table is set up well and works just fine-- it's displaying the information that I want it to, and the cells select like they should. 该表设置良好,工作正常-它正在显示我想要的信息,并且单元格按应有的方式进行选择。 However I can't get the image to change when the method didSelectRowAtIndexPath is called. 但是,当调用didSelectRowAtIndexPath方法时,我无法更改图像。

Here's what I have so far. 到目前为止,这就是我所拥有的。 I've removed extraneous code (from other tables, etc), but this should be everything that's relevant. 我已经从其他表等中删除了多余的代码,但这应该是所有相关的东西。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    if (tableView == diabetesVisitTable) {
        if (indexPath.section==0) {
            cell.textLabel.text = [_microvascularValues objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"Family Med Unchecked Box.jpg"];
        }

        else if (indexPath.section==1) {
            cell.textLabel.text = [_macrovascularValues objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"Family Med Unchecked Box.jpg"];
        }

        else if (indexPath.section==2) {
            cell.textLabel.text = [_bloodSugarValues objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"Family Med Unchecked Box.jpg"];
        }

        cell.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
        self.diabetesVisitTable.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];

        return cell;
    }
    else return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    cell.imageView.image = [UIImage imageNamed:@"Family Med Checked Box.jpg"];
}

In your tableView:didSelectRowAtIndexPath: implementation, you're asking the table view for a new cell to configure with dequeueReusableCellWithIdentifier: . tableView:didSelectRowAtIndexPath:实现中,您要向表视图询问要使用dequeueReusableCellWithIdentifier:配置的新单元格。 You want to fetch the actual cell used for that indexPath so you can update it directly: 您想要获取用于该indexPath的实际单元格,以便可以直接更新它:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"Family Med Checked Box.jpg"];
}

You'll also need to update the data source for the table view somehow to record the fact that one of the rows is selected, and update tableView:cellForRowAtIndexPath: to set the checked image if the data source indicates that the row is selected. 您还需要以某种方式更新表视图的数据源,以记录选择了某行的事实,并且如果数据源指示已选择该行,则更新tableView:cellForRowAtIndexPath:以设置选中的图像。 If you don't do those steps, the cell will appear unselected when it refreshes. 如果不执行这些步骤,则刷新时该单元将显示为未选中状态。

You're trying to update the cell at the didSelectRowAtIndexPath function which returns void. 您正在尝试更新didSelectRowAtIndexPath函数中的单元格,该函数返回void。 So it will not update the tableview. 因此它不会更新tableview。

You should take the indexpath.row value from the diddSelectRowAtIndexPath and pass it to the cellForRowAtIndexPath. 您应该从diddSelectRowAtIndexPath中获取indexpath.row值,并将其传递给cellForRowAtIndexPath。 if they're matching, update the cell, it will work. 如果它们匹配,则更新单元格,它将起作用。

Also make sure you add a beginUpdate and endUpdate to reflect the table value reload 还要确保添加一个beginUpdate和endUpdate以反映重新加载表值

UITableViewDelegate has 3 methods to handle cell highlighting: UITableViewDelegate有3种方法来处理单元格突出显示:

- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:

Try like this : 尝试这样:

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    return YES;
}

- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"Highlightimage"];    
}

- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"image"];  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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