简体   繁体   English

当数据作为JSON传入时,将数据从tableViewController保存并发送到detailViewController的正确方法是什么?

[英]What's the proper way of saving and sending data from tableViewController to detailViewController when data are coming as JSON

I was wondering, what is the "correct" approach to saving and sending data. 我想知道,保存和发送数据的“正确”方法是什么? Let's say, we receive JSON. 假设我们收到JSON。 It's structure is something like: 它的结构类似于:

  {
    id = 1;
    country_id = 298;
    date = "2012-11-08 14:00:36";
    name = "Mr. Blank";
    profile = "http://somewhere.com/user1234/profile.png";
  }

Now, we have tableView, where in CustomCell we display profile pic, name and country id. 现在,我们有了tableView,其中在CustomCell中显示了个人资料图片,名称和国家/地区ID。 On click, we want to transition into detail view, where we show profile picture again, and some other information based on his id (basically new api call). 单击时,我们要切换到详细信息视图,在该视图中再次显示个人资料图片,以及基于其ID的其他一些信息(基本上是新的api调用)。

Now, when we receive this first JSON, we should think about how we save and send values like id and profile pic. 现在,当我们收到第一个JSON时,我们应该考虑如何保存和发送id和profile pic之类的值。

I know 2 approaches, but I'm not sure, if they are "correct". 我知道2种方法,但是我不确定它们是否“正确”。 First one is, save everything into arrays as soon as your json arrives. 第一个是,在json到达后立即将所有内容保存到数组中。 So it looks something like this 所以看起来像这样

NSMutableArray *idArr = [[NSMutableArray alloc]init];
NSMutableArray *picArr = [[NSMutableArray alloc]init];

    for (NSDictionary *recDict in jsonResults)
        {
            NSString *personsID = [recDict objectForKey:@"id"];
            [idArr addObject:personsID];
            NSString *pic = [recDict objectForKey:@"profile"];
            [picArr addObject:pic];

        }

This method works, however, it always saves everything what we get in JSON into arrays. 此方法有效,但是,它始终将我们在JSON中获得的所有内容保存到数组中。 So when there are a lot of rows, it takes long time (especially if picture is big). 因此,当有很多行时,它会花费很长时间(尤其是图片很大时)。 I know, that loading asynchronously may be solution, but I want to talk about principle here. 我知道,异步加载可能是解决方案,但是我想在这里谈谈原理。

Another method is passing values straight from selected cell. 另一种方法是直接从选定的单元格传递值。 That means, doing it in method didSelectRowAtIndexPath 这意味着,在方法didSelectRowAtIndexPath中进行此操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        MyCustomCell *cell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    imageToSend = cell.profilePic.image; //we have UIImage *imageToSend;
    idToSend = cell.idLabel.text; //we have NSString *idToSend;
    [self performSegueWithIdentifier:@"showDetail" sender:self];
}

Advantage of this approach seems to be, that tableView uses "lazyload" by default(that means, it loads cells as user scrolls down) so in my opinion it saves time when compared to first approach. 这种方法的优势似乎在于,默认情况下tableView使用“ lazyload”(这意味着它在用户向下滚动时加载单元格),因此,我认为与第一种方法相比,它节省了时间。

Please could you give me some insight as how you pass values? 请问您如何传递价值观,请给我一些见解? I know that you should use prepareForSegue method, but you don't have access to indexPath.row there. 我知道您应该使用prepareForSegue方法,但是您没有访问indexPath.row的权限。 (So you can identify from where that segue is being called) (因此,您可以识别从何处调用该segue)

I like the second one better. 我更喜欢第二个。 You don't need to save all the images, just the URLs. 您不需要保存所有图像,只需保存URL。 The images should be loaded asynchronously (I use EGOImageLoading ) and then you can either send the image or the URL to the new view. 图像应该异步加载(我使用EGOImageLoading ),然后您可以将图像或URL发送到新视图。

You can use the tableView indexPathForSelectedRow to pass data from that row: 您可以使用tableView indexPathForSelectedRow从该行传递数据:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    YourObject *yourObject = [objectsArray objectAtIndex:path.row];
    [segue.destinationViewController setObject:yourObject];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM