开发者

remove all sub image views of a tableview

开发者 https://www.devze.com 2023-01-20 14:07 出处:网络
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....

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


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


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:...];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号