如何推动从必须从一个以.json文件解析的对象一个UITableViewCell一个mapKit看法?
问题描述:
@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
我有一个UITableView控制器嵌入在故事板一个TabViewController并加入细胞中以编程表视图。该电池具有从互联网上传.json文件下载的对象。我想被点击时,它的mapKit观点推到每个单独的小区,但林不知道如何做到这一点,因为我不能在故事板分别连接电池。如何推动从必须从一个以.json文件解析的对象一个UITableViewCell一个mapKit看法?
答
您需要使用didSelectRowAtIndexPath
和performSegueWithIdentifier
。
- (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
答
您回应
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];
}
我不明白。使用委托方法didSelectRowAtIndexPath方法? –