[英]Error returning cell from tableView
我在ios中使用表视图相对较新。 我正在尝试使用不同的视图编辑数据并更新原始视图中的值。 我设置了单元格标识符并编写了以下代码
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}
但是我收到以下错误:
NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
您需要检查并确保dequeueReusableCellWithIdentifier
能够使单元格出列。 它崩溃了,因为它不会每次都返回一个单元格。 如果您无法将可重复使用的单元格出列,则需要创建一个新单元格。 您的代码应如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.