4

我已经设置了UIViewController一个表格视图并添加了单元格。委托和数据源链接到表视图。但是我收到了这个错误,我很困惑为什么。当我将它与 a 一起使用UITableViewController但不与 a一起使用时,该代码有效UIViewcontroller。想要使用 a 的原因UIViewController是导航栏在使用 a 时会随着表格滚动,UITableViewController我被建议在这里使用 aUIViewController并添加表格视图。

#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;
}
4

2 回答 2

14

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

应该是这样的:

- (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;
}
于 2013-04-11T21:05:49.640 回答
1

我认为问题在于 dequeueReusableCellWithIdentifier 方法。我在视图控制器而不是 UITableViewController 中尝试了一个带有 tableView 的快速示例项目,并遇到了您描述的错误。当我使用手动生成单元格时

[UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"];

它工作得很好。

于 2013-04-11T19:39:08.927 回答