0

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

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 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; }
4

1 回答 1

0

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

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

如果您愿意,请查看Table View Programming guide。它不会受伤。

于 2012-05-24T07:32:06.830 回答