如何从UITableView单元格中删除图像?

时间:2011-09-08 11:10:30

标签: iphone uitableview uiimageview

我的表格视图以非编辑模式显示...

enter image description here

在编辑模式中......

enter image description here

我想在编辑模式下的部门名称字段中显示列表图像。它应该在非编辑模式下隐藏。

我使用tableView: cellForRowAtIndexPath: ..

中的以下代码添加了此图片
if (isInEditMode) {

    UIImageView *listingImage = [ [UIImageView alloc] initWithFrame:CGRectMake(275, 16, 13, 13)];

    listingImage.image = [UIImage imageNamed:@"listing.png"];

    [cell.contentView addSubview:listingImage];

    [listingImage release];

}

else {

    //Need to remove image from cell

}

点击右侧栏按钮(编辑按钮)更新isInEditMode 布尔值。

如何在非编辑模式下删除图像?

先谢谢

3 个答案:

答案 0 :(得分:4)

if (isInEditMode) {

UIImageView *listingImage = [ [UIImageView alloc] initWithFrame:CGRectMake(275, 16, 13, 13)];

listingImage.image = [UIImage imageNamed:@"listing.png"];
listingImage.tag = 777;
[cell.contentView addSubview:listingImage];

[listingImage release];

}

else {
    [[cell viewWithTag:777] removeFromSuperview];

}

如果[[cell viewWithTag:777] removeFromSuperview];无效,请尝试使用[[cell.contentView viewWithTag:777] removeFromSuperview];

试试这个,看看是否有帮助

答案 1 :(得分:1)

如果您使用的是自定义UITableViewCell,则可以将此UIImage添加为该自定义单元格的属性。当您需要隐藏/删除它时,您可以通过此属性访问它并隐藏/删除它。

e.g。 [cell.listImage setHidden:YES];

答案 2 :(得分:0)

使用[cell.contentView removeSubview:listingImage]

这将要求您在某处保留对listingImage的引用 - 添加一个ivar来存储它。

或者,为每个状态返回不同类型的单元格,一个包含图像,另一个没有。

相关问题