views:

38

answers:

2

i am using a table view and adding small images(cross arrow an accept arrow images) to right side of each cell based on if the option is correct or not....

....Now my table reloaded many times....due to which the images are repeated on every single cell....

i mean if cell_1 has that accept image..after table reload if the value of cell_1 changed and it require cross_arrow image...then both images are visible there...(new one above old one).....

i think it was due to sub view was added to it last time which was not removed..

please tell me how to remove that old image

here is some of my code which adds image to cell...in delegate method of tableView

UIImageView *image =[[UIImageView alloc]initWithFrame:CGRectMake(260,5,40,40)];
if(correct)
    [image setImage:[UIImage imageNamed:@"right.png"]];
else
    [image setImage:[UIImage imageNamed:@"wrong.png"]];
image.autoresizingMask;
image setBackgroundColor:[UIColor clearColor]];
[cell addSubview:jimage];
[jobStatus release];

please note i have to use only my images and not table's accessory types..

and there are no fixed numbers of rows in my table view (created at runtime).

therefore i also cannot use a class imageView.

please help

+1  A: 

Set the tag of the UIImageView so you can access it later on with

[imageView setTag:10];

Then you can change the image of the image view from the table view cell by calling

[[tableViewCell viewWithTag:10] setImage:[UIImage imageNamed:@"wrong.png"]];

Another solution: display the image on the right side of the cell you can set the accessory view of the table view cell

[tableViewCell setAccessoryView:imageView];

which then let's you access the image view with

[(UIImageView*)tableViewCell.accessoryView setImage:...];
brutella
+1--i want to try second one but last line confusing me..please give some more light...
Ranjeet Sajwan
First we are setting the accessory view of the table view cell to our image view, then we can access the image view through the accessoryView property. Because the accessoryView is a UIView we have to cast it to UIImageView* so we can use the setImage: image without compiler warnings.
brutella
+1  A: 

Use a similar approach like when you dequeue cells: Ask the cell for its imageView, and if it's not there create it.

#define kTagCellImageView 42

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"EditorCell";

    UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UIImageView *imageView = [cell viewWithTag:kTagCellImageView];
    if (imageView == nil) {
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(260,5,40,40)];
        imageView.backgroundColor = [UIColor clearColor];
        imageView.tag = kTagCellImageView;
        [cell addSubview:imageView];
    }
    if (correct)
        [imageView setImage:[UIImage imageNamed:@"right"]];
    else 
        [imageView setImage:[UIImage imageNamed:@"wrong"]];
    cell.textLabel.text = [[textLabels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}
fluchtpunkt
+1--Thank you it works.....
Ranjeet Sajwan