我正在尝试使用 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/