依赖于字典中值的表视图样式单元格



我拼命想弄清楚如何根据字典中的值设置Tableview样式单元格。字典是一个SQLite行,我已经设置了一列来设置要使用的单元格(类型列)然后这应该告诉cellForRowAtIndexPath使用哪种单元格样式。在我看来,这段代码应该没有问题,但我不断收到以下错误:

由于未捕获的异常而终止应用 'NSInternalInconsistencyException', reason: 'UITableView dataSource 必须从 tableView:cellForRowAtIndexPath:' 返回一个单元格

任何帮助都会很棒!非常感谢,安德鲁

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
dictionary = [tableDataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    if ([dictionary objectForKey:@"Type"] == @"1") {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.numberOfLines = 0;
        cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
    }
    else if ([dictionary objectForKey:@"Type"] == @"0") {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    }
}
// Configure the cell...
cell.textLabel.text = [dictionary objectForKey:@"Title"];
cell.detailTextLabel.text = [dictionary objectForKey:@"Right_Info"];
return cell; }

为了比较两个NSString对象,您应该使用isEqualToString:方法。使用 == 运算符,您可以比较两个指针。

此外,dequeueReusableCellWithIdentifier:已经返回了UITableViewCell的实例,因此您不应尝试再次分配一个实例。

如果您愿意,请查看表视图编程指南。不会痛的。

最新更新