将NSArray的内容加载到UITableView中

时间:2014-08-10 16:21:02

标签: ios objective-c uitableview nsarray

我填写了名为** teacherNames *的NSArray。使用以下内容从名为Teachers的Core数据实体成功导入数据:

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'

任何人都可以建议我做错了吗?

1 个答案:

答案 0 :(得分:4)

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