Ios 核心数据:NSFetchedResultsController
这是我的数据模型 在我的TableView中,我想显示第一个和第二个城市 下面是我获取它们的代码。我得到的是第一批数据,但不是城市: 这是我获取结果的地方,下面是我尝试显示获取结果的地方Ios 核心数据:NSFetchedResultsController,ios,core-data,nsfetchedresultscontroller,Ios,Core Data,Nsfetchedresultscontroller,这是我的数据模型 在我的TableView中,我想显示第一个和第二个城市 下面是我获取它们的代码。我得到的是第一批数据,但不是城市: 这是我获取结果的地方,下面是我尝试显示获取结果的地方 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"TableCell";
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
-(void)configureCell:(TableCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *obj = [fetchedResultsController objectAtIndexPath:indexPath];
// cell.lblName.text = @"";
[cell.lblName setText:[obj valueForKey:@"first"]];
// cell.lblCity.text = @"";
[cell.lblCity setText:[obj valueForKey:@"city"]];
}
获取的结果控制器配置为返回Person对象,因此您需要间接引用关系的属性:
[cell.lblCity setText:[obj valueForKeyPath:@"address.city"]];
或者,如果您已经创建了NSManagedObject的子类,例如Person,则可以将代码修改为:
Person *obj = [fetchedResultsController objectAtIndexPath:indexPath];
cell.lblName.text = obj.first;
cell.lblCity.text = obj.address.city;
@VNJ抱歉,我使用了valueForKey:我应该使用valueForKeyPath:-我已经修改了我的答案以更正。
Person *obj = [fetchedResultsController objectAtIndexPath:indexPath];
cell.lblName.text = obj.first;
cell.lblCity.text = obj.address.city;