我是iphone的新手。滚动表格视图时,我的cutomcell label
值正在消失。
再次出现在我点击该单元格时。
任何人都可以帮助我吗?
先谢谢。
//table view in view controller created in xib
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ListOfProductsCell";
ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"ListOfProductsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
productItemDit=[productListArry objectAtIndex:indexPath.row];
NSString *offerStr= [NSString stringWithFormat:@"%.2f",[[productItemDit objectForKey:@"offer"] floatValue]];
NSString *fullCostStr=[[currencyCodeStr stringByAppendingString:@" "] stringByAppendingString:offerStr];
NSLog(@"%@",fullCostStr);
cell.itemCostLbl.text=fullCostStr;
} else {
cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];
}
if (tableView == self.searchDisplayProduct.searchResultsTableView) {
searchProductItemDit=[searchProductListArry objectAtIndex:indexPath.row];
NSLog(@"searchdit:%@",searchProductItemDit);
cell.itemNameLbl.text= [searchProductItemDit objectForKey:@"name"];
self.searchDisplayProduct.searchResultsTableView.separatorColor=[UIColor colorWithRed:200.0 green:0.0 blue:0.0 alpha:1.0];
} else {
productItemDit=[productListArry objectAtIndex:indexPath.row];
NSLog(@"dit:%@",productItemDit);
cell.itemNameLbl.text=[productItemDit objectForKey:@"name"];
}
cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:@"%d",itemCount];
cell.itemImg.image = [UIImage imageNamed:@"profp.jpg"];
return cell;
}
答案 0 :(得分:1)
通过这样做解决
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ListOfProductsCell";
ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"ListOfProductsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
} else {
cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];
}
if (tableView == self.searchDisplayProduct.searchResultsTableView) {
cell.itemNameLbl.text= [[searchProductListArry objectAtIndex:indexPath.row] objectForKey:@"name"];
} else {
cell.itemNameLbl.text=[[productListArry objectAtIndex:indexPath.row] objectForKey:@"name"];
}
cell.itemCostLbl.text=[NSString stringWithFormat:@"%@ %.2f", currencyCodeStr , [[[productListArry objectAtIndex:indexPath.row] objectForKey:@"offer"] floatValue]];
cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:@"%d",itemCount];
cell.itemImg.image = [UIImage imageNamed:@"profp.jpg"];
return cell;
}
答案 1 :(得分:0)
由于您没有发布大量信息,这是我的猜测。当您滚动表视图时,它会在内部调用reloadData方法。您需要使用重用标识符来保留单元的状态。您可以在cellForRowAtIndexPath中初始化单元格,如:
static NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// or cell initializing logic here for the custom cell
}
如果自定义单元格在xib中添加重用标识符并在cellForRowAtIndexPath方法中使用它