我正在创建一个通用的iPhone/iPad应用程序,我希望在主视图上具有自定义单元格。它适用于iPhone,但在iPad上dequeueReusableCellWithIdentifier:返回nil而不是单元格。下面是一些我的代码(我代替我自定义的细胞与正常的UITableViewCell,因为它不反正工作):iOS通用应用程序 - 自定义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 Storyboard中没有任何MainPhoneCell重用标识符,我在iPad故事板中有MainPadCell,但无法找到它)。
感谢您的任何建议!
我已经解决了这个问题,此代码的工作很好,谢谢大家的前瞻性和应答。 – haluzak 2013-03-06 10:54:44
检查,看是否有用户在iPhone或iPad中的cellForRowAtIndexPath是一个做事的真正低效的方式。只要显示单元格,此方法就会触发。潜在的数千次。 – 2013-03-06 11:26:07