Son*_*ter 5 objective-c uitableview ios
我发现桌子上发生了一些奇怪的事.我想创建具有两个或更多部分的表,并且在第一部分我想要使用与其他部分不同的自定义单元格.
所以我在我身上创造了这个 tableView:cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
if (indexPath.section == 0) {
// cell for section one
HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!headerCell) {
[tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
headerCell.labelName.text = @"First Section";
return headerCell;
}
else {
// Cell for another section
DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!detailSection) {
[tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
detailCell.textLabel.text = @"Another Section Row";
return detailCell;
}
}
Run Code Online (Sandbox Code Playgroud)
在第一部分,我想headerCell用于我的行,然后用于detailCell其他行.这段代码有效,但在第二部分的行看起来仍然使用headerCell"under" detailCell.我添加了标签headerCell.xib,它仍然显示在detailCell.看到这个图像.
我认为这一切都是因为我为所有部分使用了一个单元格标识符.谁有解决方案?非常感谢.
每种类型的自定义单元格都应具有自己的唯一标识符.您的代码尝试对所有单元使用相同的单元标识符.那不行.
另外,注册两种细胞类型viewDidLoad,而不是cellForRowAtIndexPath:.
试试这个:
static NSString *cellIdentifier0 = @"cell0";
static NSString *cellIdentifier1 = @"cell1";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
// cell for section one
HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0 forIndexPath:indexPath];
headerCell.labelName.text = @"First Section";
return headerCell;
} else {
// Cell for another section
DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath];
detailCell.textLabel.text = @"Another Section Row";
return detailCell;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// the rest of your code
[self.tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier0];
[self.tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier1];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5704 次 |
| 最近记录: |