0

调试器向我抛出了这个错误

UITableView dataSource必须从return_celltableView: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;
}

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

4

2 回答 2

4

cell从来没有设置在里面case LOCATION_SECTION:

此外,有时您cell.textLabel.text在块中设置一个值case,只是nil稍后通过cell.textLabel.text = text. 这肯定不是故意的?

于 2010-03-02T18:26:38.103 回答
-1

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

于 2010-03-02T22:20:43.773 回答