【问题标题】:How to remove image from the UITableView cell?如何从 UITableView 单元格中删除图像?
【发布时间】:2011-09-08 11:10:29
【问题描述】:

我的表格视图在非编辑模式下看起来...

在编辑模式中...

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

我在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布尔值相对于右栏按钮(编辑按钮)的点击。

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

提前致谢

【问题讨论】:

    标签: iphone uitableview uiimageview


    【解决方案1】:
    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];

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

    【讨论】:

    • 另外,在tableView: cellForRowAtIndexPath: 中包含此代码效果很好...for (UIView *view in cell.contentView.subviews) { [view removeFromSuperview]; } 我只是从单元格中删除所有子视图..
    • 为什么要删除所有子视图并重新添加。您可能会影响性能。
    【解决方案2】:

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

    例如[cell.listImage setHidden:YES];

    【讨论】:

    • 感谢您的回复.. 但我没有在这里使用自定义 UITableViewCell。我应该怎么做才能清除图像?
    【解决方案3】:

    使用[cell.contentView removeSubview:listingImage]

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

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

    【讨论】:

    • 感谢您的回复。“或者,为每个状态返回不同类型的单元格,一个有图像,一个没有”我无法理解这个..你能用代码解释一下吗?
    • 查看自定义 UITableViewCells developer.apple.com/library/ios/#documentation/UserExperience/… 上的文档。本质上在tableView: cellForRowAtIndexPath: 方法中,决定返回两个自定义表格视图单元中的哪一个
    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多