简体   繁体   中英

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. 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.

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: . You want to fetch the actual cell used for that indexPath so you can update it directly:

-(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. 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. So it will not update the tableview.

You should take the indexpath.row value from the diddSelectRowAtIndexPath and pass it to the 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

UITableViewDelegate has 3 methods to handle cell highlighting:

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

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