カスタム UITableviewCells をスクロールすると、間違ったコンテンツ (別のセルのテキスト) が表示されます。これはランダムに発生します。セルをクリアしようとしましたが、空白のセルが表示されます。私のコードは以下です。誰が何が起こっているのか教えてもらえますか。ここや他の場所から、セルをクリアする必要がある多くの記事を読みましたが、どの時点でデータをクリアするのかよくわからないため、うまくいきませんでした。セルのクラスに 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;
}