TableView Datasource must return a cell, check code
The debugger thr开发者_Python百科ew me this error
UITableView dataSource
must return
a cell
from tableView:cellForRowAtIndexPath:
this is my code:
- (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;
}
I just can't seem to spot the error, and I could use a set of fresh eyes.
cell
is never set inside case LOCATION_SECTION:
.
Also, sometimes you set cell.textLabel.text
to a value in a case
block, only to reset it to nil
later via cell.textLabel.text = text
. Surely this isn't intended?
Ok, now i just feel silly, it seems that i left out an important snippet of code involving the dequeueing of cell in the LOCATION_SECTION case. I added it and the error went away, and i would like to thank "rpetrich" for pointing out something i left in the code from a previous time "cell.textlabel.text = text;", and this too needed to be removed, so THANK YOU.
精彩评论