简体   繁体   中英

Reusing multiple custom tableview cell

Hi guys im using multiple tableview cells.one for textfiled ,another for images.and another for button.the thing is custom tableview cell created is not getting reused and the cell is getting nil every single time.could u guys help me out.below is the code.

   -(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;
    }

A few things here.

1 ) in each of the XIB files that host your table view cells, make certain you've set the identifier on them to match what you're using in your " cellForRowAtIndexPath " method. EG " @"Cell3 " for RatingCells.

2) You need to register the cell reuse identifier for the object you're loading from a XIB.

For example:

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

You probably need to do this before you start calling " cellForRowAtIndexPath " (at least that's what I do). Perhaps you could send the " tableView " parameter in to your various " get*Cell " methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM