iphone - 使用 dequeueReusableCellWithIdentifier 会导致用作重复背景的图像泄漏出单元格

标签 iphone ios uitableview uiimageview uiimage

我正在尝试使用 3 张图像作为单元格的背景。顶部图像,中间(重复图像)和底部图像。如果我使用 dequeueReusableCellWithIdentifier 那么它工作正常,但是一旦我开始滚动表格,图像就不会留在单元格内。

见附图。

http://theutherfish.co.uk/iosss.png

这是麻烦的代码

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellidentifier"];
}

这工作正常,但我认为它是一个内存消耗。

UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];

两全其美的最佳方式是什么?也就是说,它可以正常工作并且不会占用大量内存。

更新 索引路径函数处的行的完整单元格

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellidentifier"];
}

UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 0.0, 310.0, 8.0)];
img1.image = [UIImage imageNamed:@"top.png"];

UIImageView *img2 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 8.0, 310.0, [PullRefreshTableViewController heightForExpandingCell:text])];
img2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"middle.png"]];

UIImageView *img3 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, [PullRefreshTableViewController heightForExpandingCell:text] + 8.0, 310.0, 8.0)];
img3.image = [UIImage imageNamed:@"tempbottom.png"];

[[cell contentView] addSubview:img1];
[[cell contentView] addSubview:img2];
[[cell contentView] addSubview:img3];

return cell;

最佳答案

我建议使用单个可拉伸(stretch)图像作为单元格背景,并仅在创建单元格时(即当单元格== nil 时)将其添加到单个 ImageView 中(使用适当的调整大小的蒙版)。

然后您可以在 heightForRowAtIndexPath 方法中调整高度。

目前,每次重复使用一个单元格时,您都会添加 ImageView ,因此您最终会在每个单元格中添加数十个 ImageView ,并且它们的高度各不相同,因此看起来一团糟。

如果可拉伸(stretch)图像不适合您,您应该考虑仅按上述方法创建一次 ImageView ,并在重新使用单元格时简单地调整框架。

或者,由于您的图像看起来只不过是一些灰色圆角,请查看 CALayer 的 borderWidth、borderColor、borderRadius 属性。

关于iphone - 使用 dequeueReusableCellWithIdentifier 会导致用作重复背景的图像泄漏出单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8862133/

相关文章:

iphone - 如何通过拖动 UIButton 来启动 IBAction?

ios - NSTimer 在后台模式下工作——但它不应该不工作吗?

ios - 如何使表格滚动工作 : UIControl within UITableView within UIControl

arrays - 存储在数组中并显示在 UITableView 中的 UISearchBar 结果与我的 didSelectRowAt indexPath 方法混淆(Swift)

iphone - isa 是弃用问题

iphone - 从 iphone 将 XML 数据发布到 Web 服务器

javascript - 如何使用 Google map API 进行搜索?

ios - 按下按钮后记录时数据 (char*) 出现损坏

ios - NSOperationQueue 序列化 NSURLConnection 请求

ios - Labels 和 Imageviews 在 iOS7 的自定义单元格中表现异常