gpt4 book ai didi

ios - 将 NSArray 的内容加载到 UITableView 中

转载 作者:行者123 更新时间:2023-11-28 19:01:15 24 4
gpt4 key购买 nike

我已经填充了一个名为 **teacherNames* 的 NSArray。数据成功来自名为 Teachers 的核心数据实体,使用以下内容:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Teachers"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teachers" inManagedObjectContext:self.managedObjectContext];

request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"teacherName" ascending:YES]];
request.entity = entity;
request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"teacherName"]];

NSError *error = nil;
self.teacherNames = [self.managedObjectContext executeFetchRequest:request error:&error];
[self.tableViewTeacherNames reloadData];

当我在调试窗口中键入以下内容时,我可以看到有:

print self.teacherNames.count

它返回数组中的正确数字。

然而,当我尝试使用 NSArray 来填充表格时:

- (UITableViewCell *)tableView:(UITableView *)tableViewTeacherNames cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"teacherCell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSString *currentItem = [[self.teacherNames objectAtIndex:indexPath.row] objectForKey:@"teacherName"]; - ERROR LINE
cell.textLabel.text = currentItem;
return cell;
}

我收到这个错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject objectForKey:]: unrecognized selector sent to instance 0xb88ce30'

谁能指出我做错了什么?

最佳答案

尝试使用 valueForKey: 而不是 objectForKey:。后者属于 NSDictionary 而前者属于任何 NSObject 后代

关于ios - 将 NSArray 的内容加载到 UITableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25230606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com