我发现它可以用不同的 xib 创建不同的单元格,但我想知道是否可以在一个 xib 中创建不同的单元格?如果可能,我应该怎么做?
这是代码,我知道这是错误的代码,所以你们能帮我修正一下吗?
- (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;
}
}
最佳答案
我的第一个想法是不这样做。但如果我必须这样做,我想我会尝试使用旧式出列来构建单元格,并通过手动从 Nib 中提取它们来构建可重用单元格的初始池。
所以,不要做任何 cell nib 或 cell class 注册,然后在 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;
关于ios - 是否可以在一个 xib 中创建多个单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34503951/