简体   繁体   English

核心数据逆关系

[英]Core Data Inverse RelationShip

I am creating Travel guide app using core data.我正在使用核心数据创建旅行指南应用程序。 I have 4 entities CITY ,RESTAURANTS , HOTEL AND FAMOUS PLACES.我有 4 个实体城市、餐厅、酒店和著名的地方。 City is connected with all other entity because one city may have number of restaurants , hotels and places.城市与所有其他实体相连,因为一个城市可能有许多餐馆、酒店和场所。 City entity has 3 attribute Name ,Image ,Description.城市实体具有 3 个属性 Name 、Image 、Description。 I am able to display list restaurants of selected city.In Restaurant Entity I have 4 attribute Name, Description ,Address and phone no..Now I want to show this attribute detail of selected restaurant(of selected city) in next view.But how can I access restaurant description in next view.. Here is my code.我能够显示所选城市的列表餐厅。在餐厅实体中,我有 4 个属性名称、描述、地址和电话号码。现在我想在下一个视图中显示所选餐厅(所选城市)的此属性详细信息。但是如何我可以在下一个视图中访问餐厅描述吗.. 这是我的代码。

     - (void)viewWillAppear:(BOOL)animated {

        [super viewWillAppear:animated];

        NSMutableArray *restaurants = [[NSMutableArray alloc]initWithArray:[city.cityTorestaurants allObjects]];





        NSSortDescriptor *nameDescriptor  = [[NSSortDescriptor alloc]initWithKey:@"Name" ascending:YES];

        NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:nameDescriptor,nil];
        [restaurants sortUsingDescriptors:sortDescriptors];
        [hotels sortUsingDescriptors:sortDescriptors];
        [self setArrayOfRestaurants:restaurants];
        [restaurants release];
        [nameDescriptor release];
        [sortDescriptors release];
        [tableView reloadData];

    }

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil ) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier]autorelease];

    }

    //set up cell


    NSLog(@"For Restaurants List");
    Restaurants *restaurants = [arrayOfRestaurants objectAtIndex:indexPath.row];
cell.textLabel.text = restaurants.Name;

return cell; 
} 

cityTorestaurants and restaurantTocity is relationship in core data.. Help Please.. cityTorestaurants 和 restaurantTocity 是核心数据中的关系.. 帮助请..

In your RestaurantViewController create a property Restaurant* currentRestaurant在您的 RestaurantViewController 中创建一个属性Restaurant* currentRestaurant
in your didSelectRowAtIndexPath: of the CityViewController set the property with self.restaurantVC.currentRestaurant = [arrayOfRestaurants objectAtIndex:indexPath.row];在你的didSelectRowAtIndexPath:的 CityViewController 中设置属性self.restaurantVC.currentRestaurant = [arrayOfRestaurants objectAtIndex:indexPath.row];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM