我已设置UIViewController
,其中包含表格视图和单元格。委托和数据源链接到表视图。但是我收到这个错误,我很困惑为什么。当我使用UITableViewController
而不是UIViewcontroller
时,代码可以使用。想要使用UIViewController
的原因是导航栏在使用UITableViewController
时随表格滚动,我建议在这里使用UIViewController
并添加表格视图。< / p>
#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;
}
答案 0 :(得分:13)
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;
}
答案 1 :(得分:1)
我认为问题是dequeueReusableCellWithIdentifier方法。我在视图控制器而不是UITableViewController中尝试了一个带有tableView的快速示例项目,并遇到了您描述的错误。当我使用
手动生成单元格时 [UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"];
它运作良好。