user1191343
user1191343

Reputation: 129

Change UIImage of table cells on touch

I have this setting below, but when I touch the table cell, I get the alert, so I know the method is being called but the image on the left of the cell isn't changing. This is what my code looks like: In the view controller I have these 2 methods:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
{

    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"homeworkcell"];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"homeworkcell"];

    }
    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];

    //static NSString *CellIdentifier = @"Cell";

       /* if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:CellIdentifier];
    }*/

    // Configure the cell.
    //----------------------------------START----------------------Set image of cell----
    cellImage = [UIImage imageNamed:@"checkboxblank.png"];

    cell.imageView.image = cellImage;

    //-------------------------------END---------------------------end set image of cell--  

    return cell;

}

cellImage is declared in the .h and I synthesized it as well in this .m

right below the above method, I have this one(i change the image at the bottom:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
 {
     NSString *cellselected = [NSString stringWithFormat:@"cell selected %@", [tabledata    objectAtIndex:indexPath.row]];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:cellselected  delegate:self cancelButtonTitle:@"close" otherButtonTitles:nil];

    [alert show];
    //------------------------------------------------------------------------------start
     cellImage = [UIImage imageNamed:@"checkboxfull.png"];
    //----------------------------------------------------------------------------end

}

Need all the help I can get, thanks!

Upvotes: 3

Views: 1760

Answers (2)

Shubhank
Shubhank

Reputation: 21805

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{

    NSString *cellselected = [NSString stringWithFormat:@"cell selected %@", [tabledata    objectAtIndex:indexPath.row]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:cellselected  delegate:self cancelButtonTitle:@"close" otherButtonTitles:nil];

    [alert show];
    [alert release];

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

Upvotes: 3

beryllium
beryllium

Reputation: 29767

You have forgotten to change imageView:

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

Upvotes: 1

Related Questions