什么是正确的forms来configurationUITableViewCell?
我在我的cellForRowAtIndexPath
方法中使用ReusableCell
,
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
对象正在configuration。 滚动之后,我的问题就开始了。
在Cell-1中,rootImageView.image应该是Image-1在Cell-2中,rootImageView.image应该是Image-2在Cell-3中,rootImageView.image应该是Image-3
当我开始滚动和我的代码出来if(_cell == nil)
,我的所有图像转回像Image-1相同的图像。 但是,当我logging[_cellArray description]
,数据是完全正确的。 我错过了什么? 为什么细胞不是零后,所有的细胞图像都回来了Image-1?
编辑:这真的很奇怪,如果我不使用kCellID图像加载正确。 我将_cell
属性切换到HomePageCell *cell
。
正如其他人已经指出,它看起来像你存储_cell
作为一个实例variables。 从来不这样做,因为每个单元实例都必须按照要求在屏幕上显示的方式出厂/创build。
如果你的UITableViewCell
实例是一个自定义的子类(我认为它是),你应该重写它的-prepareForReuse
方法,你应该把-prepareForReuse
的image
属性设置为nil
。 否则,你可以在你的-tableView: cellForRowAtIndexPath:
实现中为它赋值一个nil
值。
另外,由于单元格被快速排队/重用/丢弃的方式,您应该检查是否在从networking到达右单元格的时候分配请求的图像。 要做到这一点,而不是保持对单元格的弱引用(就像你在做的那样),在networking请求完成块内获得一个具有所需索引path的单元的新引用。
总之,你的实现应该是这样的:
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; }
我认为_cell
是你class上的一个属性,这是错误的。 永远不要将一个单元存储为一个属性,它并不意味着被存储! 只需创build一个局部variables并将图像分配给该variables。 这也不是一个好主意,但至less应该起作用。
UITableView
的理论是你提供单元格一次 ,那么你不要修改它! 如果你想,你重新加载表格视图,它会再次询问你的单元格, 然后你可以修改这种方式。 你不应该存储任何types的指针到一个UITableViewCell
或它的任何子类。
我假设你从cellData
获得这个URL,在这种情况下,问题是你不重置这个属性,所以当一个单元格被重用时,你仍然使用initWithStyle:reuseIdentifier:withDictionary:
的同一个cellData
initWithStyle:reuseIdentifier:withDictionary:
方法。 你应该这样做:
if(_cell == nil) { _cell = [[HomePageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID withDictionary:cellData]; } else { [_cell setCellData:cellData]; }