Iphone 为什么不是';我的桌面视图没有加载吗?
我有以下代码,其中我相信Iphone 为什么不是';我的桌面视图没有加载吗?,iphone,ios,tableview,xcode4.2,storyboard,Iphone,Ios,Tableview,Xcode4.2,Storyboard,我有以下代码,其中我相信NSFetchRequest实际上正在工作,但是视图运行后,我的tableView(peopleList)中没有显示任何内容 -(void)viewWillAppear:(BOOL)animated{ AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *moc = [appDelegate managedObjectCon
NSFetchRequest
实际上正在工作,但是视图运行后,我的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”,这让我觉得故障在于让数组本身显示在界面中。要让我的角色出现在人物列表中需要做什么
非常感谢您的帮助。如果fetch正在工作,那么我猜您还没有为tableview设置数据源和/或委托。您需要声明一个UITableViewDataSource,它是一个实现该协议并向表提供数据的对象。这就是调用CellForRowatineXpath的部分。它通常是自组织的,即tableViewController类,因为它通常将数组作为局部变量
myTableView.dataSource = self;
在头文件中,执行以下操作:
@interface myTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
}
@接口myTableViewController:UITableViewController{
}
我收到一个“正在传递”MainScreen*const_strong到不兼容类型“id”错误的参数。您的表视图控制器是否声明它实现了UITableViewDataSource协议?看我上次的编辑。不,不是,但我已经改正了。现在它抛出了一个异常,问题在于声明行“cell.textlab.text=cellValue;”。控制台显示“NSInvalidArgumentException”,原因:“-[Person isEqualToString:]:未识别的选择器发送到实例0x912f2c0”。personArray中有什么类型的对象?它们是NSString实例还是某种Person类?如果是Person类,则需要执行类似Person.lastName的操作。它是一个具有两个属性firstName和lastName的Person类。