【发布时间】:2015-10-02 15:31:52
【问题描述】:
我需要有关在 UITableViewController 中加载自定义 UITableViewCell 的帮助。预期的结果是有一个 UITableViewController,其中包含 3 个 UITableViewCell:3 个中有 2 个是常规 UITableViewCell,一个是自定义 UITableView 子类,名为 SubscriptionContentCell。根据内容存在、不存在或仍在从服务器加载的条件显示 3 个单元格。我对条件没有问题,但对库存有问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
我在其中放置代码以选择适当的 UITableViewCell 的方法。这是代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier;
id cell;
if ([deactivableOrNotContents count] > 0) {
cellIdentifier = @"subscriptionCell";
} else {
if (getPurchasedHasFired == true) {
cellIdentifier = @"noContentsAvailable";
} else {
cellIdentifier = @"loadingCell";
}
}
if ([deactivableOrNotContents count] > 0) {
SubscriptionContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = contentCell;
} else {
UITableViewCell *standardCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = standardCell;
}
// Configure the cell...
if (cell == nil) {
if ([deactivableOrNotContents count] > 0) {
cell = [[SubscriptionContentCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
}
return cell;
}
我遇到的问题是SIGABRT在线:
SubscriptionContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
即使我导入了课程。
这是我的故事板文件中 UITableViewController 内的 UITableView 单元格:
我的代码有什么问题?
【问题讨论】:
-
为什么要进行两次 if 检查?你可以一次性完成它们
-
可能单元格标识符变为零,因为您将它们分配在不同的位置
-
检查 XIB 看看是否有任何
IBOutlets 集不再存在于您的代码中。 -
您是否在情节提要上设置单元格标识符?
-
@Aluminum 太好了——当然,我刚刚发布了一个更详细的答案。
标签: ios objective-c uitableview