【问题标题】:Pass data to new view from tableView将数据从 tableView 传递到新视图
【发布时间】:2013-09-18 16:18:23
【问题描述】:

我有一个表格视图,它会在单元格点击时推送详细视图。新视图推送但是我试图将网址传递给加载 webView 的详细视图,但我不确定如何执行此操作。这是我的代码:

-(void)setupArray {
    webAddress = [[NSMutableDictionary alloc]init];
    [webAdress setObject:@"http://www.google.com.au" forKey:@"first item"];
    [webAdress setObject:@"http://www.yahoo.com.au" forKey:@"second item"];

menuItems = [webAdress allKeys];

}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
return cell;

}

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detailView animated:YES];
} 

任何帮助将不胜感激。

【问题讨论】:

    标签: ios uitableview xcode4.5


    【解决方案1】:

    您需要在详细视图中为此使用属性并在 didSelectRowAtIndexPath 方法中传递 URL,如下所示:

    - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
       DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
       detailView.webAddress = [menuItems objectAtIndex:indexPath.row];
       [self.navigationController pushViewController:detailView animated:YES];
    }
    

    在您的 DetailView 中:

    @property (nonatomic, strong) NSString *webAddress;
    

    【讨论】:

      【解决方案2】:

      您应该向DetailViewController 添加一个实例变量,该变量将保存地址并将其设置为所需的值。然后(使用适当的访问器)在推送视图之前/之后设置它。

      例如如果您将变量定义为webAddress:

      DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
      [detailView setWebAddress:[webAddress objectForKey:[menuItems objectAtIndex:[indexPath row]]]]; // or some other way to get the correct address
      [self.navigationController pushViewController:detailView animated:YES];
      

      唯一剩下要做的就是将 Web 视图的地址设置为 viewWillAppear: 上的这个值或其他东西。

      编辑:

      viewWillAppear: 方法示例如下所示:

      - (void)viewWillAppear:(BOOL)animated
      {
          // construct request
          NSURL *url = [NSURL URLWithString:webAddress];
          NSURLRequest *req = [NSURLRequest requestWithURL:url];
          [webView loadRequest:req];
      
          [super viewWillAppear:animated];
      }
      

      【讨论】:

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