当我滚动自定义UITableviewCells时,会得到错误的内容(另一个单元格的文本)。这种情况是随机发生的。我试着清理牢房,但后来我得到了一些空白牢房。我的代码在下面。有人能告诉我发生了什么事吗。我在这里和其他地方读过很多关于需要清除单元格的文章,但没有一篇对我有效,因为我真的不确定你在什么时候清除数据。我甚至尝试在cell的类中实现prepareForReuse,但效果不佳。
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.products count] == 0) {
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}
static NSString *CellIdentifier = @"AvailableCustomerProductCell";
AvailableCustomerProductTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.buttonAdd.tag = indexPath.row;
[cell.buttonAdd addTarget: self action: @selector(addToSelectedProduct:) forControlEvents: UIControlEventTouchUpInside];
Product *prod = nil;
if (theTableView == self.searchDisplayController.searchResultsTableView) {
prod = (Product *)[self.filteredProducts objectAtIndex:indexPath.row];
} else {
prod = (Product *)[self.products objectAtIndex:indexPath.row];
}
if (prod != nil) {
cell.pNumber.text = prod.number;
cell.description.text = prod.desc;
if ([Common getProductPromotion:prod] != nil) {
cell.btnPromotionTag.hidden = NO;
cell.btnPromotionTag.tag = indexPath.row;
[cell.btnPromotionTag addTarget: self action: @selector(showPromotionDetails:) forControlEvents: UIControlEventTouchUpInside];
}
else{
cell.btnPromotionTag.hidden = YES;
}
//Get the customer product price, first:
//If if the product has a record in the productCustomerPrices list
//if not get the price from the standard price.
if (self.order.orderOrderCustomer != nil) {
CustomerPrice *custPrice = [Common getPriceForCustomer:self.order.customerRef forProduct:prod.productId];
if (custPrice != nil) {
//get the customer price
[cell.btnPrice setTitle:[Common getCurrencyFormattedStringFromFloat:[custPrice.price floatValue]] forState:UIControlStateNormal];
[cell.btnPrice setTitleColor:[UIColor colorWithRed:0.01 green:0.65 blue:0.77 alpha:1] forState:UIControlStateNormal];
cell.btnPrice.enabled = NO;
}else{
//get the standard price
float price =[[Common GetProductStandardPrice:prod.productStanddardPrices ByQuantity:[NSNumber numberWithInt:1]] floatValue];
[cell.btnPrice setTitle: [Common getCurrencyFormattedStringFromFloat:price] forState:UIControlStateNormal ];
[cell.btnPrice setTitleColor:[UIColor colorWithRed:1.0 green:0.39 blue:0.0 alpha:1] forState:UIControlStateNormal];
cell.btnPrice.tag = indexPath.row;
[cell.btnPrice addTarget: self action: @selector(showStandardPrices:) forControlEvents: UIControlEventTouchUpInside];
cell.btnPrice.enabled = YES;
}
}
}
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:sgr];
return cell;
}
您的问题几乎可以肯定与表视图回收单元格有关。正如你所说,这就是细胞需要"清除"的原因
例如,如果在顶部附近有一个显示图像的单元格,如果向下滚动,并且该单元格用于显示不应该显示图像的单元,则该图像仍将显示,除非此后删除该图像。即使你有100个单元格要显示,实际存在的实例也可能只有少数——它们会被回收。
话虽如此,即使您没有说明哪个文本仍在出现,如果prod
是nil
,它可能是各种对象,包括pNumber
和description
。如果self.order.orderOrderCustomer
是nil
,情况也是如此。为了避免这种情况,您可以在获得cell
:后立即放置以下内容
cell.pNumber.tex = @"";
cell.description.text = @"";
//etc
另一个注意事项:您正在向手机的buttonAdd
按钮添加一个目标。你应该删除之前行上的现有操作。例如:
[cell.buttonAdd removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.buttonAdd addTarget: self action: @selector(addToSelectedProduct:) forControlEvents: UIControlEventTouchUpInside];
btnPromotionTag
和btnPrice
也是如此。
确保在每次cellForRowAtIndexPath
调用中重置单元的状态。您有一堆if/else条件,并且控制流使得根本不会调用用于在单元格上设置内容的代码,这就是为什么以前重用的单元格中的内容仍然存在的原因。
我的规则是,在if
s中,我总是有匹配的else条件来更改单元格的状态(如果没有,则至少为空)。对于UITableViewCell
子类,您可以在prepareForReuse
中重置状态
通常,如果有数百条记录,最好对所有单元格使用相同的标识符,这样单元格就可以重用内存。否则,如果表视图足够长,并且用户在其中快速滚动,将导致大量的分配/取消分配,这将导致滚动不平滑。然后总是将特定于行的代码放在检查单元格为nil的条件之外。
看看这个代码片段:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"MY_CELL_IDENTIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
// Everything that is similar in all the cells should be defined here
// like frames, background colors, label colors, indentation etc.
}
// everything that is row specific should go here
// like image, label text, progress view progress etc.
return cell;
}