【发布时间】:2013-02-21 01:53:38
【问题描述】:
我正在创建一个通用的 iPhone/iPad 应用程序,我想在主视图上有自定义单元格。它适用于 iPhone,但在 iPad 上 dequeueReusableCellWithIdentifier: 返回 nil 而不是单元格。这是我的一些代码(我用普通的 UITableViewCell 替换了我的自定义单元格,因为它无论如何都不起作用):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
CellIdentifier = @"MainPadCell"; //works fine when replaced with MainPhoneCell
__weak UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //returns nil
[cell.textLabel setText:@"Test"];
return cell; //crash
}else{
CellIdentifier = @"MainPhoneCell";
__weak UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.textLabel setText:[NSString stringWithFormat:@"Cell %i", indexPath.row]];
return cell;
}
}
奇怪的是,当我用 MainPhoneCell 替换 MainPadCell 单元格时,它会显示手机单元格(虽然我的 iPad 故事板中没有任何 MainPhoneCell 重用标识符,但我在 iPad 故事板中有 MainPadCell 并且找不到它) .
感谢您的建议!
【问题讨论】:
-
我已经解决了问题,这段代码运行良好,感谢大家的关注和回答。
-
检查用户在 cellForRowAtIndexPath 中是否有 iPhone 或 iPad 是一种非常低效的处理方式。每当显示单元格时,都会触发此方法。可能上千次。
标签: ios ipad uitableview ios-universal-app