【问题标题】:Displaying data on DrawerMenu Tableview在 DrawerMenu Tableview 上显示数据
【发布时间】:2016-08-15 13:19:47
【问题描述】:

我一直在尝试使用这个 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"]);
    }

}

并在 MenuViewController 的表格视图中显示“名称”,并在选中单元格时将 url 传递给 ViewController 的 Web 视图。

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

【问题讨论】:

    标签: ios objective-c uitableview uiwebview


    【解决方案1】:

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

    @interface menuViewController:UIViewController <UITableViewDelegate, UITableViewDataSource>
    
    @property(nonatomic) NSArray *menuArray;
    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    
    @end
    

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

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

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

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

    现在,实现表格视图数据源方法

    - (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;
    }
    

    现在,您应该已将您的表格设置好并显示名称。现在要将 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];
    }
    

    【讨论】:

    • 我有多个这样的链接 //Present it [self presentViewController:vc animated:YES completion:nil];只允许我导航一次。这条线的替代品是什么?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多