@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视图推送到每个单独的单元格,但我不知道该怎么做,因为我无法在故事板中单独连接单元格。
答案 0 :(得分:0)
您需要使用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
答案 1 :(得分:0)
您的回复
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];
}