[英]Why isn't my tableView loading?
我有以下代碼,我相信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;
}
以防萬一,我的情節提要如圖所示。
我的控制台顯示“請求計數為23”,這使我認為故障在於使數組本身顯示在界面中。 使我的personArray出現在peopleList中,該怎么辦?
任何幫助表示贊賞。
您需要聲明一個UITableViewDataSource,這是一個實現該協議並將數據提供給表的對象。 這是調用cellForRowAtIndexPath的片段。 它通常是self -ie tableViewController類,因為它通常將數組作為局部變量。
myTableView.dataSource = self;
然后在頭文件中執行以下操作:
@interface myTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
}
如果您的提取工作正常,那么我猜您還沒有為表視圖設置數據源和/或委托。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.