为什么我的tableView加载不?

时间:2011-11-30 04:22:09

标签: iphone ios tableview xcode4.2 storyboard

我有以下代码,我相信NSFetchRequest实际上正在运行,但在viewWillAppear运行后我的tableView(peopleList)中没有显示任何内容。

-(void)viewWillAppear:(BOOL)animated{

  AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *moc = [appDelegate managedObjectContext]; 

  NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" 
                                                     inManagedObjectContext:moc];

  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:entityDescription];


  NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
  [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];


  NSError *error = nil;

  NSLog(@"Count for request is %a", [moc countForFetchRequest:request error:&error]);

  personArray = [moc executeFetchRequest:request error:&error];

  [peopleList reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *cellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier];
  }

  NSString *cellValue = [personArray objectAtIndex:indexPath.row];
  cell.textLabel.text = cellValue;

  return cell;

}

以防万一,我的故事板如图所示。

enter image description here

我的控制台说“Count for request is 23”,让我觉得故障是在让阵列本身显示在界面中的某个地方。让personArray出现在peopleList中需要做些什么?

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您需要声明一个UITableViewDataSource,它是一个实现该协议并向您的表提供数据的对象。这是调用cellForRowAtIndexPath的部分。它常常是自我的。 tableViewController类,因为它通常将数组作为局部变量。

myTableView.dataSource = self;

在您的标头文件中,执行以下操作:

 @interface myTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {

 }

答案 1 :(得分:0)

如果你提取工作正常,那么我猜你没有为tableview设置数据源和/或委托。

相关问题