0

我制作了关于运行 iPhone 5S、Xcode 6 和 iOS 8 的教程。我想在单元格表中显示 textDetailLabel。你能帮我看看这里有什么问题吗?我已经检查了语法,但它不起作用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Checklist *checklist = [self.dataModel.lists objectAtIndex:indexPath.row];
cell.textLabel.text = checklist.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

int count = [checklist countUncheckedItems];
if ([checklist.items count] == 0) {
    cell.detailTextLabel.text = @"(No Items)";
} else if (count == 0) {
    cell.detailTextLabel.text = @"All Done!";
} else {
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Remaining", count];
}
return cell;

}

4

1 回答 1

2
  1. 移动

    静态 NSString *CellIdentifier = @"Cell";

出方法,公开。

  1. 当您创建表格视图时,在您创建表格的位置写下这一行(在 viewDidLoad 或您创建表格的方法中)

[dataTable registerClass:[UITableViewCell class] forCellReuseIdentifier:tableIdentifier];

  1. 删除单元格条件

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

方法,因为您的表格视图单元格不是 nil 并且您无法设置单元格样式。

所以你必须写

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
于 2015-07-28T12:23:23.847 回答