UItableviewcell在ios中重用问题

你好,我认为几乎每个在ios开发中的人都可能会遇到使用下面的代码行来重复使用UITableCell的问题。

RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

我有这方面的search很多,但没有得到任何愿望的答案,所以请帮助我在这种情况下。

我有同样的问题,大部分的iPhone开发者重新使用单元格后。 我有我的单元格内的UIProgressView和一个button是在那里下载的video,我显示了进展中的进展情况视图剩下多less。 所以,现在我有什么问题是当我有更多的数据,并在那个时候,我正在按下UITableviewCell的第一行下载button,然后我滚动下来,所以进展也显示在底部随机一个单元格UI在两个单元中改变,而不是一个。

您需要在自定义单元类中实现-prepareForReuse方法,并将所有单元属性设置为默认值。

 - (void)prepareForReuse 

如果一个UITableViewCell对象是可重用的 – 也就是说,它有一个重用标识符 – 这个方法在从UITableView方法dequeueReusableCellWithIdentifier:返回对象之前被调用。 出于性能原因,您应该只重置与内容无关的单元格的属性,例如,alpha,编辑和select状态。 tableView:cellForRowAtIndexPath:中的表视图委托应该在重用单元时始终重置所有内容。 如果单元对象没有关联的重用标识符,则不调用此方法。 如果重写此方法,则必须确保调用超类实现。

请参阅这里更多, https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse

您需要在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath分配一个进度值- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // If the cell is reused, the `-prepareForReuse:` of `UITableViewCell` will be called. //!! Assign current progress value to this cell, otherwise, the progressBar.value may look like a random value. //!! Because the current cell is reused from a disappeared cell. cell.progressBar.value = ... ; return cell; } 

devise可能很复杂,因为当单元格在屏幕上时,进度可能会不断更新。

使用prepareforreuse method clear单元格的内容

 -(void)prepareForReuse { [super prepareForReuse]; self.textLabel.text = @""; self.detailTextLabel.text = @""; self.imageView.image = nil; }