Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

ios - UITableView with two custom cells (multiple identifiers)

I am trying to put a cell as a space between each cell - which will be hidden by setting alpha = 0. In my table, the space cells will be for rows that are odd.

Note that the actual cell height is 85, but the hidden cell height (ie space between cells) is 20.

The problem is that the space cell height is 85, but not 20, I don't know why. Maybe the cell is not loaded correctly.

Cell here is the UITableViewCell - the actual cell - with identifier 'Cell'.

Cell2 is the space with identifier 'Space'.

Each class above has its own UITableViewCell class and the XIB files are also assigned to each of them. The identifier is also set in the IB for each Xib.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"Cell";
static NSString *CellIdentifier2 = @"Space";

Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

if(!cell)
{
    NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil];
    for (id obj in ar)
    {
        if ([obj isKindOfClass:[Cell class]])
        {
            cell = (Cell *)obj;
            break;
        }
    }
}

if (indexPath.row % 2 == 1)
{
    Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

    if (!cell2)
    {
        NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"Cell2" owner:nil options:nil];
        for(id obj in ar)
        {
            if([obj isKindOfClass:[Cell2 class]])
            {
                cell2 = (Cell2 *)obj;
                break;
            }
        }

        // Method 1
        cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
         // Method 2
        //cell2 = [[Cell2 alloc] init];
        // Method 3
        //cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        [cell2.contentView setAlpha:0];
        // prevent selection and other stuff
        [cell2 setUserInteractionEnabled:NO];
    }
    return cell2;
}
else
{
    // Configure the actual cell
}


return cell;

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

* I've renamed some of your NIB/Class names for a better understanding. *

First, you should register each cells' NIB:

- (void)viewDidLoad{
    [super viewDidLoad];

    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];

    nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];

    self.contentView.hidden = YES;
    [self loadData];
}

Because you have the NIBs registered, dequeueReusableCellWithIdentifier: will always return a cell:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    // Space Cell
    if (indexPath.row % 2 == 1) {
        CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        return cell;
    }

    // Content cell
    else {
        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        // Configure cell
        return cell;
    }
}

Last, but not least, make sure to implement the following delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Space cell's height
    if (indexPath.row % 2 == 1) {
        return 20.0f;
    }

    // Content cell's height
    else {
        return 80.0f;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...