在 DrawerMenu Tableview 上显示数据

Displaying data on DrawerMenu Tableview

我一直在尝试使用这个 repo 的示例项目来试验在两个不同的视图控制器之间传递数据:https://github.com/RajendrasinhParmar/DrawerMenu

我已经从 GitHub 下载了演示代码,我一直在尝试使用以下方法解析此演示 JSON:

{
  "menu": [
    {
      "name": "Facebook",
      "url": "http://www.facebook.com"
    },
    {
      "name": "Twitter",
      "url": "http://www.twitter.com"
    }
  ]
}

#pragma mark - http stuff
- (void)getMenuList {
    NSError *error;
    NSString *url_string = [NSString stringWithFormat: @"http://json-schema-faker.js.org/#gist/6a6cc18dc58dca786194f390c0af28c9"];
    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
    NSMutableDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSMutableArray *menuArray = response[@"menu"];
//    NSLog(@"response: %@", response);

    for (int i=0; i < menuArray.count; i++) {
        NSLog(@"%@", menuArray[i][@"name"]);
        NSLog(@"%@", menuArray[i][@"url"]);
    }

}

并在 table 视图的菜单 ViewController 中显示 "name" 并在选择单元格时将 url 传递到 ViewController 的网络视图。

我的问题是。如何在 Tableview 中分配名称 属性 并在选择时将 url 传递给 webview?我做了一些搜索,但我失败了。

将 menuArray 设为全局。然后用数组计数给出行数,并使用索引路径访问每个对象,现在它肯定小于 table view 中的对象数。使用 urlString 在下一个视图控制器中创建 属性 并传递 didSelectRowAtIndexPath 行中的值(为此,您需要子类化 viewController)。

首先,在您的 menuViewController 的界面中创建一个数组 属性,并将 table 视图委托和数据源添加到您的界面声明中。您还需要在界面生成器中向 menuViewController 添加一个 table 视图,并将其连接到您的 .h 文件。

@interface menuViewController:UIViewController <UITableViewDelegate, UITableViewDataSource>

@property(nonatomic) NSArray *menuArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

接下来,在获得 JSON 序列化后,分配给您在接口中定义的数组,然后重新加载您的 table 视图

NSMutableDictionary *response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
self.menuArray=response[@"menu"]; //Assign to array
[self.tableView reloadData];

现在,要设置 table,首先在 viewDidLoad 方法中分配数据源和委托

- (void)viewDidLoad {
  self.tableView.dataSource=self;
  self.tableView.delegate=self;
}

现在,实现 table 查看数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  //Want as many cells as in array
  return self.menuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"menuCell"];
  if (cell==nil) {
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
  }
  //Display name of current item in array
  cell.textLabel.text=[[self.menuArray objectAtIndex:indexPath.row]objectForKey:@"name"];      

  return cell;
}

现在,您的 table 应该已完全设置并显示名称。现在要将 url 传递给下一个控制器,您需要实现另一个方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  //Create new controller 
  ViewController *vc=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
  //Pass it the url
  vc.url=[[self.menuArray objectAtIndex:indexPath.row]objectForKey:@"url"];
  //Present it
  [self presentViewController:vc animated:YES completion:nil];
}