TableView数据源必须返回一个单元格,检查代码

时间:2010-03-02 18:20:26

标签: iphone objective-c cell tableview

调试器把这个错误扔给了我

UITableView dataSource必须return cell来自tableView:cellForRowAtIndexPath:

这是我的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell   = nil;
    NSString        *text   = nil;
    NSInteger       section = indexPath.section;
    NSInteger       row     = indexPath.row;

    switch (section) 
    {
        case PURCHASE_SECTION:
        {   
            static NSString *cellID = @"GenericCell";

            cell = [tableView dequeueReusableCellWithIdentifier:cellID];

            if (cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                               reuseIdentifier:cellID] autorelease];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }

            switch (row) 
            {
                case CATEGORY_ROW:
                    text                        = [self.purchase.category valueForKey:@"name"];
                    cell.textLabel.text         = text;
                    cell.accessoryType          = UITableViewCellAccessoryNone;
                    cell.editingAccessoryType   = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case TYPE_ROW:
                    text                        = [self.purchase.type valueForKey:@"name"];
                    cell.textLabel.text         = text;
                    cell.accessoryType          = UITableViewCellAccessoryNone;
                    cell.editingAccessoryType   = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case VENDOR_ROW:
                    text                        = [self.purchase.vendor valueForKey:@"name"];
                    cell.textLabel.text         = text;
                    cell.accessoryType          = UITableViewCellAccessoryNone;
                    cell.editingAccessoryType   = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case NOTES_ROW:
                    text                        = @"Notes";
                    cell.textLabel.text         = text;
                    cell.accessoryType          = UITableViewCellAccessoryNone;
                    cell.editingAccessoryType   = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                default:
                    break;
            }
            break;
        }
        case ITEMS_SECTION:
        {
            NSUInteger  itemsCount = [purchase.items count];

            if (row < itemsCount) 
            {
                static NSString *itemsCellID = @"ItemsCell";

                cell = [tableView dequeueReusableCellWithIdentifier:itemsCellID];

                if (cell == nil)
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                                   reuseIdentifier:itemsCellID] autorelease];
                    cell.accessoryType = UITableViewCellAccessoryNone;
                }

                PurchaseItem *item          = [items objectAtIndex:row];
                cell.textLabel.text         = item.name;
                cell.detailTextLabel.text   = [item.amount formattedDataDisplay];
            } 
            else 
            {
                static NSString *AddItemCellID = @"AddItemCell";

                cell = [tableView dequeueReusableCellWithIdentifier:AddItemCellID];

                if (cell == nil) 
                {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                                   reuseIdentifier:AddItemCellID] autorelease];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                }
                cell.textLabel.text = @"Add Item";
            }
            break;
        }
        case LOCATION_SECTION:
        {
            text = @"Purchase Location";
            cell.accessoryType          = UITableViewCellAccessoryDisclosureIndicator;
            cell.editingAccessoryType   = UITableViewCellAccessoryNone;
            break;
        }
        default:
            break;
    }
    cell.textLabel.text = text;
    return cell;
}

我似乎无法发现错误,我可以使用一双新鲜的眼睛。

2 个答案:

答案 0 :(得分:4)

cell永远不会在case LOCATION_SECTION:内设置。

此外,有时您会将cell.textLabel.text设置为case块中的值,但稍后会通过nil将其重置为cell.textLabel.text = text。当然这不是故意的吗?

答案 1 :(得分:-1)

好的,现在我觉得很傻,似乎我省略了一个重要的代码片段,涉及LOCATION_SECTION案例中的单元格出列。我添加了它并且错误消失了,我想感谢“rpetrich”指出我之前在代码中留下的东西“cell.textlabel.text = text;”,这也需要删除,所以,谢谢。