【问题标题】:How to push a mapKit view from a UITableViewCell that has objects parsed from a .json file?如何从具有从 .json 文件解析的对象的 UITableViewCell 推送 mapKit 视图?
【发布时间】:2018-02-04 18:41:53
【问题描述】:
@property NSArray* restaurants;

@end

@implementation RestaurantsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self requestRestaurants];
}

- (void)requestRestaurants {
    NSString *urlString = @"https://s3-us-west-2.amazonaws.com/samwell.party/yasminucla/restaurants.json";
    NSURL *url = [NSURL URLWithString:urlString];

    NSData *restaurantData = [NSData dataWithContentsOfURL:url];

    self.restaurants = [NSJSONSerialization JSONObjectWithData:restaurantData
                                                       options:NSJSONReadingAllowFragments
                                                         error:nil];
    [self.tableView reloadData];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.restaurants count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSInteger index = indexPath.row; // Get the row that we're at
    NSString* restaurant = self.restaurants[index]; // Get the name of the restaurant for the row from the array
    cell.textLabel.text = restaurant; // Set the cell text to be the name of the restaurant

    return cell;
}

@end

我在情节提要的 TabViewController 中嵌入了一个 UITableView 控制器,并以编程方式将单元格添加到表格视图中。这些单元格具有从 Internet 的 .json 文件下载的对象。我想在单击时将 mapKit 视图推送到每个单独的单元格,但我不知道该怎么做,因为我无法在情节提要中单独连接单元格。

【问题讨论】:

  • 没看清楚。使用委托方法 didselectRowAtIndexPath?

标签: ios objective-c storyboard mapkit segue


【解决方案1】:

您需要使用didSelectRowAtIndexPathperformSegueWithIdentifier

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"yourSegue" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *mapViewController = segue.destinationViewController;
    NSString* restaurant = self.restaurants[index];
    mapViewController.title = restaurant;
}

另请参阅:How to make a push segue when a UITableViewCell is selected

【讨论】:

    【解决方案2】:

    您的回复

        self.storyboard    =      [
                                 "Mr. Chow",
                                 "Panera Bread",
                                 "Il Pastaio",
                                 "Jinky's Cafe",
                                 "Kazu Nori",
                                 "Happy Days"
                                  ]
    

    添加一个名为MapViewController的类,然后做一个传递json数据的属性

     #import <UIKit/UIKit.h>
    
     @interface MapViewController : UIViewController
     @property(nonatomic,strong)NSString*PassVarible;
    
     @end
    

    添加编写代码后你的初始类

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          MapViewController*vc=[self.storyboard instantiateViewControllerWithIdentifier:@"MapViewController"];
          vc.PassVarible=[self.restaurants objectAtIndex:indexPath.row];
          [self.navigationController pushViewController:vc animated:YES];
      }
    

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多