我正在组合一个 TableView,并且需要在同一个表中使用多个单元格类。
例如,我将如何在我的cellForRowAtIndexPath
method?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
我知道我可以简单地替换UITableViewCell
使用我的自定义类并使用 if 语句根据索引调整类,但这不是有点混乱吗?合理且可能是最智能的方法是什么?
当然可以。只需创建自定义单元的新实例并为其提供新的 CellId。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (condition1)
{
static NSString *CellIdentifier1 = @"Cell1";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
// TODO: if cell is null, create new cell of desired type.
// This is where you create your custom cell that is
// derived form UITableViewCell.
}
else
{
static NSString *CellIdentifier2 = @"Cell2";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
//TODO: if cell is null, create new cell of desired type
}
// Configure the cell...
return cell;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)