[英]How to configure cellForRowAtIndexPath to handle two different custom UITableViewCell types
I am trying to use two different custom UITableViewCell's but am struggling with how to get this done.我正在尝试使用两种不同的自定义 UITableViewCell,但正在努力解决如何完成这项工作。 I have the following but think that everything needs to be called cell.
我有以下内容,但认为一切都需要称为单元格。 Also, I'm not sure if my return type is correct.
另外,我不确定我的返回类型是否正确。 What is the canonical way to set this up?
设置它的规范方法是什么?
This is a follow-up to this question (although not fully relevant).这是这个问题的后续(尽管不完全相关)。
-(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;
}
Assuming that you are using prototype cells in your Storyboard, or that you have otherwise registered your cell identifiers, then your code is almost right.假设您在 Storyboard 中使用原型单元格,或者您以其他方式注册了单元格标识符,那么您的代码几乎是正确的。 You should only dequeue the type of cell you need -
你应该只出列你需要的单元格类型 -
-(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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.