是否可以在一个 xib 中创建多个单元格? [英] Is that possible to create multiple cells in one xib?
问题描述
我发现它可以用不同的 xib 创建不同的单元格,但我想知道是否可以在一个 xib 中创建不同的单元格?如果可能,我应该怎么做?
I found it can create different cells with different xibs, but I want to know is possible to create different cell in one xib? If possible, what should I can do this?
这是代码,我知道这是错误的代码,所以你们能帮我修复它吗?
Here is the code, I know it is wrong code, so can you guys help me to fix it?
- (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;
}
}
推荐答案
我的第一个想法是不要这样做.但是如果我必须这样做,我想我会尝试使用旧式出队来构建单元格,并通过手动从笔尖中提取它们来构建可重用单元格的初始池.
My first idea would be to not do it. But if I had to do it, I guess I'd try building cells by using the old style dequeue, and, to build the initial pool of reusable cells, by manually extracting them from the nib.
所以,不要做任何 cell nib 或 cell class 注册,然后在 cellForRowAtIndexPath...
So, don't do any cell nib or cell class registration, 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;
这篇关于是否可以在一个 xib 中创建多个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!