我对 cocoa 开发绝对是新手,所以请不要因为我问这个问题而责备我。
我必须使用绑定(bind)吗?我知道它可以节省大量代码,但由于我正在学习,所以我真的更喜欢在代码中设置所有内容。
我的目标是用从核心数据获取的实体填充 TableView 。我为主窗口 View 创建了一个自定义 Controller 类。我还有一些代码来获取我的类别实体
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Category"
inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *fetchedObjectsArray = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (fetchedObjectsArray == nil) {
NSLog(@"Error occured fetching categories: %@", error);
}
for (Category *cat in fetchedObjectsArray) {
NSLog(@"categories %@", cat.name);
}
我知道我必须为我的 TableView 实现委托(delegate),但我有点迷失在哪里放置我的获取代码,因此 TableView 1. 可以访问它并且 2. 使用从我的获取请求返回的数据.
最佳答案
您将获取代码放在程序中对获取有用的位置。
您可以以方便的方式存储数据,可能是 Controller 中的属性类型数组。
获取数据后,您可以通过发送 reloadData 告诉 TableView 有新数据要显示。 (等)向其发送消息。
data source对于 TableView ,您的 Controller 可能会接收数据源协议(protocol)中定义的消息,以将日期传输到 View 中。由于这也是您编写的代码,因此它完全在您的控制之下,您如何获取数据。 (参见步骤 2)。
关于objective-c - cocoa ,带有核心数据数据源的 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179431/