什么是配置 UITableViewCell 的正确形式? [英] What is correct form to configure UITableViewCell?
问题描述
我在我的 cellForRowAtIndexPath
方法中使用了 ReusableCell
;
I am using ReusableCell
in my cellForRowAtIndexPath
method like that;
NSDictionary *cellData = [_cellArray objectAtIndex:indexPath.row];
static NSString *kCellID = @"homePage";
_cell = (HomePageCell*) [tableView dequeueReusableCellWithIdentifier:kCellID];
if(_cell == nil)
{
_cell = [[HomePageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID withDictionary:cellData];
}
__weak typeof(_cell) weakSelf = _cell;
[_cell.rootImageView setImageWithURLRequest:_cell.requestImage placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
weakSelf.rootImageView.image=image;
} failure:nil];
和我的 rootImageView
对象正在配置.我的问题是这样滚动后开始;
and my rootImageView
object is configuring. My problem is start after scrolling like that;
在 Cell-1 中 rootImageView.image 应该是 Image-1在 Cell-2 rootImageView.image 应该是 Image-2在 Cell-3 中 rootImageView.image 应该是 Image-3
In Cell-1 rootImageView.image should be Image-1 In Cell-2 rootImageView.image should be Image-2 In Cell-3 rootImageView.image should be Image-3
当我开始滚动并且我的代码出来时if(_cell == nil)
,我的所有图像都变回了与 Image-1 相同的图像.但是,当我登录 [_cellArray description]
时,数据完全正确.我错过了什么?为什么在单元格不为零后所有单元格图像都变回Image-1?
When i starting scroll and my code is coming out if(_cell == nil)
, my all images turned back same image as Image-1. However, when i logged [_cellArray description]
, data is totally true. What is the point i missed? Why all cell images turned back Image-1 after cell is not nil?
这真的很奇怪,如果我不使用 kCellID 图像加载正确.我将 _cell
属性切换为 HomePageCell *cell
.
EDIT : This is really weird, if i don't use kCellID images are loading correct. I switched _cell
property to HomePageCell *cell
.
推荐答案
正如其他人已经指出的那样,您似乎将 _cell
存储为实例变量.永远不要这样做,因为每个单元格实例都必须动态"出列/创建,因为它们被要求显示在屏幕上.
As others have already pointed out, it looks like you’re storing _cell
as an instance variable. Never do that, as each cell instance must be dequeued/created ‘on the fly’ as they are requested to be displayed onscreen.
如果你的 UITableViewCell
实例是一个自定义子类(我认为是),你应该覆盖它的 -prepareForReuse
方法,你应该在其中设置 imageView
的 image
属性为 nil
.否则你可以在你的 -tableView: cellForRowAtIndexPath:
实现中为它分配一个 nil
值.
If your UITableViewCell
instance is a custom subclass (which I assume it is), you should override its -prepareForReuse
method, where you should set the imageView
’s image
property to nil
. Otherwise you could assign it a nil
value in your -tableView: cellForRowAtIndexPath:
implementation.
此外,由于单元格快速排队/重用/丢弃的方式,您应该检查是否在请求的图像从网络到达时将其分配到正确的单元格.为此,与其保持对单元格的弱引用(如您所做的那样),而是在网络请求完成块内使用所需的 indexPath 获取对单元格的新引用.
Also, because of the way that cells are quickly queued/reused/discarded, you should check that you are assigning the requested image by the time it arrives from the network to the right cell. To do so, instead of keeping a weak reference to the cell (as you’re doing), inside the network request completion block get a new reference to the cell with the desired indexPath.
总而言之,您的实现应如下所示:
In all, your implementation should look like this:
static NSString *kCellID = @"homePage"; // defined somewhere else, probably at the top of your .m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HomePageCell *cell = (HomePageCell *)[tableView dequeueReusableCellWithIdentifier:kCellID];
if(cell == nil) {
cell = [[HomePageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID withDictionary:cellData];
}
// here we reset the cell’s imageView image property to nil
// you can/should also do this inside the subclass implementation by overriding -prepareForReuse
cell.rootImageView.image = nil;
// request the remote image and set it as the imageView image property
[cell.rootImageView setImageWithURLRequest:cell.requestImage placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
// since the response could come back some time after the request,
// we need to get a fresh, new reference to the cell (which might
// have been dequeued and it’s no longer the same one we got before)
HomePageCell *aCell = (HomePageCell *)[tableView cellForRowAtIndexPath:indexPath];
aCell.rootImageView.image = image;
} failure:nil]
return cell;
}
这篇关于什么是配置 UITableViewCell 的正确形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!