Is it possible to create multiple cells in one xib?

I found that it can create different cells with different xib, but I want to know if it is possible to create another cell in one xib? If possible, what should I do?

here

Here is the code, I know this is the wrong code, so can you guys help me fix this?

- (void)viewDidLoad {
    [super viewDidLoad];

    [imageTableView registerNib:[UINib nibWithNibName:@"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"oneImageTableViewCell"];
    [imageTableView registerNib:[UINib nibWithNibName:@"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"twoImageTableViewCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier1 = @"oneImageTableViewCell";
    NSString *identifier2 = @"twoImageTableViewCell";
    if (indexPath.row % 2 == 0) {
        ImageTableViewCell *cell = [imageTableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];
        return cell;
    } else {
        ImageTableViewCell *cell = [imageTableView dequeueReusableCellWithIdentifier:identifier2 forIndexPath:indexPath];
        return cell;
    }
}
+2
source share
1 answer

My first idea is not to do this. But if I had to do this, I would try to create cells using the old dequeue style, and to create the original pool of reusable cells, manually extracting them from nib.

So, do not register cells in a cell or cell, and then in cellForRowAtIndexPath ...

NSString *identifier;
NSInteger index;
if (indexPath.row % 2 == 0) {
    identifier = @"oneImageTableViewCell";
    index = 0;
} else {
    identifier = @"twoImageTableViewCell";
    index = 1;
}
UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ImageTableViewCell" owner:self options:nil];
    cell = topLevelObjects[index];
}
return cell;
+2
source

Source: https://habr.com/ru/post/1651028/


All Articles