OGeek|极客世界-中国程序员成长平台

标题: objective-c - 如何为同一张表中的不同单元格集设置不同的 UITableViewCell 样式? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:04
标题: objective-c - 如何为同一张表中的不同单元格集设置不同的 UITableViewCell 样式?

您好,我正在实现一个 UITableView。我打算根据特定条件对不同的单元格集使用不同的 UITableViewCellStyle(请参见下面的代码片段)

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifierlaceholderCellIdentifier]autorelease];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    cell.textLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.numberOfLines = 0;
}

if ([genericObj.type isEqualToString"question"]) 
{
    //I want to user UITableViewCellStyleValue1
}
else if([genericObj.type isEqualToString"topic"])
{
    //I want to user UITableViewCellStyleValue2
}
else //user
{
    //I want to user UITableViewCellStyleSubtitle
}

谁能告诉我如何相应地更改 uitableviewcellstyle?



Best Answer-推荐答案


在 UITableViewCell 初始化后,您无法更改其样式。

改为使用不同的重用标识符。

将 if then else 放在 if (cell == nil) 行的上方。从这些队列中取出您需要的不同类型的单元格。

如果单元格为 nil,则在另一个 if then else block 中初始化您需要的单元格。

关于objective-c - 如何为同一张表中的不同单元格集设置不同的 UITableViewCell 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964594/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4