UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格

equia 发布于 2019-03-09 ios 最后更新 2019-03-09 14:38 0 浏览

我已经建立了UIViewController,并添加了表格视图和单元格。委托和数据源链接到表视图。但是,我得到这个错误,我很困惑,为什么。当我使用UITableViewController而不是UIViewcontroller时,该代码正常工作。想要使用UIViewController的原因是导航栏在使用UITableViewController时随表格一起滚动,我在这里建议使用UIViewController并添加Table视图。

#import "ObViewControllerObservationsTable.h"
#import "ObAppDelegate.h"
#import "Observations.h"
#import "ObViewControllerTableDetail.h"
#import "ObViewControllerMainMenu.h"
@interface ObViewControllerObservationsTable ()
@property (nonatomic, strong) NSArray *teacherNames;
@property (nonatomic, readonly) NSManagedObjectContext *managedObjectContext;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *detailsButton;
@end
@implementation ObViewControllerObservationsTable
- (void)viewDidLoad
{
[super viewDidLoad];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Observations"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"obsTeacherName" ascending:YES]];
NSError *error = nil;
self.teacherNames = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
[self.tableView reloadData];
}
- (NSManagedObjectContext *) managedObjectContext
{
return [(ObAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.teacherNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"teacherCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
cell.textLabel.text = currentList.obsID;
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.teacherNames objectAtIndex:indexPath.row]];
    [self.managedObjectContext save:nil];
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Observations"];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"obsTeacherName" ascending:YES]];
    NSError *error = nil;
    self.teacherNames = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (self.teacherNames.count == 0)
    {
        self.detailsButton.enabled = NO;
    }
    else
    {
        self.detailsButton.enabled = YES;
    }
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
obsID = [[self.teacherNames objectAtIndex:indexPath.row] obsID];
self.detailsButton.enabled = YES;
}
已邀请:

malias

赞同来自:

如果可重用单元队列中不存在可重用对象,则dequeueReusableCellWithIdentifier:返回nil。 (参见UITableView reference)。 这是应该如何:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"teacherCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // More initializations if needed.
    }
    Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
    cell.textLabel.text = currentList.obsID;
    return cell;
}

jalias

赞同来自:

我认为问题是dequeueReusableCellWithIdentifier方法。我在视图控制器而不是UITableViewController中尝试了一个带有tableView的快速示例项目,并遇到了您描述的错误。当我使用手动生成单元格时 [UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"]; 它工作得很好。