我正在尝试使用两个不同的自定义 UITableViewCell,但正在为如何完成这项工作而苦苦挣扎。我有以下但认为一切都需要被称为细胞。另外,我不确定我的返回类型是否正确。设置它的规范方法是什么?
这是 this question 的后续行动(虽然不完全相关)。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MenuHeaderCellIdentifier=@"HeaderCell";
static NSString *MenuItemCellIdentifier=@"ItemCell";
HeaderCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
ItemCell *miCell = [self.menuTV dequeueReusableCellWithIdentifier:ItemCellIdentifier];
id dic=self.tmpMenu.listItems[indexPath.row];
if([dic isKindOfClass:[Header class]]){
Header *menuHeader=(Header *)dic;
cell.nameLabel.text=@"here";
return cell;
}else if([dic isKindOfClass:[Item class]]){
Item *item=(Item *)dic;
miCell.headerLabel.text=@"here";
return miCell;
}
最佳答案
假设您在 Storyboard 中使用原型(prototype)单元格,或者您已经注册了单元格标识符,那么您的代码几乎是正确的。你应该只出列你需要的单元格类型 -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *menuHeaderCellIdentifier=@"HeaderCell";
static NSString *menuItemCellIdentifier=@"ItemCell";
id dic=self.tmpMenu.listItems[indexPath.row];
if([dic isKindOfClass:[Header class]]) {
HeaderCell *headerCell = [self.menuTV dequeueReusableCellWithIdentifier:menuHeaderCellIdentifier];
Header *menuHeader=(Header *)dic;
headerCell.nameLabel.text=@"here";
return headerCell;
} else if([dic isKindOfClass:[Item class]]) {
ItemCell *itemCell = [self.menuTV dequeueReusableCellWithIdentifier:menuItemCellIdentifier];
Item *item=(Item *)dic;
itemCell.headerLabel.text=@"here";
return itemCell;
}
关于ios - 如何配置 cellForRowAtIndexPath 以处理两种不同的自定义 UITableViewCell 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25375317/