0

大家好,我正在使用多个表格视图单元格。一个用于文本文件,另一个用于图像。另一个用于按钮。问题是创建的自定义表格视图单元格没有被重用,并且单元格每次都为零。你们能帮帮我吗。下面是代码。

   -(ProductCell *)getProductCell
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ProductCell" owner:nil options:nil];
    ProductCell  *cell;
    for (id currentObject in topLevelObjects)
    {
        if ([currentObject isKindOfClass:[ProductCell  class]])
        {
            cell= (ProductCell*)currentObject;
            return cell;
        }
    }
    return nil;
}
-(RatingCell *)getRatingCell
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RatingCell" owner:nil options:nil];
    RatingCell  *cell;
    for (id currentObject in topLevelObjects)
    {
        if ([currentObject isKindOfClass:[RatingCell  class]])
        {
            cell= (RatingCell*)currentObject;
            return cell;
        }
    }
    return nil;
}

-(NotifyCell *)getNotifyCell
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NotifyCell" owner:nil options:nil];
    NotifyCell  *cell;
    for (id currentObject in topLevelObjects)
    {
        if ([currentObject isKindOfClass:[NotifyCell  class]])
        {
            cell= (NotifyCell *)currentObject;
            return cell;
        }
    }
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier1 = @"Cell1";
    static NSString *CellIdentifier2 = @"Cell2";
    static NSString *CellIdentifier3 = @"Cell3";

    self.productCell=(ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    self.notifyCell = (NotifyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    self.ratingCell =(RatingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier3];

    if (productCell == nil) productCell = [self getProductCell];
    if (notifyCell == nil) notifyCell = [self getNotifyCell];
    if (ratingCell == nil) ratingCell = [self getRatingCell];

    switch (indexPath.section) {
        case 0:
        self.productCell.lblName.text=[productTitleArray objectAtIndex:indexPath.row];
        return productCell;
        break;
        case 1:
        self.productCell.lblName.text=[invoiceTitleArray objectAtIndex:indexPath.row];
        return productCell;
        break;
        case 2:
        self.productCell.lblName.text=[warrantyTitleArray objectAtIndex:indexPath.row];
        return productCell;
        break;
        case 3:
        self.productCell.lblName.text=@"Description";
        return productCell;
        break;
        case 4:
        self.ratingCell.lblName.text=@"Rating";
        self.ratingCell.starRatingControl.delegate=self;
        return self.ratingCell;
        break;
        case 5:
        self.productCell.lblName.text=[serviceContactsTitleArray objectAtIndex:indexPath.row];
        return productCell;
        break;
        case 6:
        self.notifyCell.lblName.text=[notifyTitleArray objectAtIndex:indexPath.row];
        return notifyCell;
        break;
        default:
        break;
    }
    return productCell;
    }
4

1 回答 1

1

这里有几件事。

1 ) 在托管表格视图单元格的每个 XIB 文件中,确保您已在它们上设置标识符以匹配您在 " cellForRowAtIndexPath" 方法中使用的内容。EG " @"Cell3" 代表 RatingCells。

2) 您需要为您从 XIB 加载的对象注册单元重用标识符。

例如:

UINib * nib = [UINib nibWithNibName: @"RatingCell" bundle: nil];
if(nib)
{
    [tableView registerNib: nib forCellReuseIdentifier:@"Cell3"];
}

您可能需要在开始调用“ ”之前cellForRowAtIndexPath执行此操作(至少我是这样做的)。也许您可以将“ tableView”参数发送到您的各种“ get*Cell”方法中。

于 2013-05-15T22:02:07.123 回答