I was wondering, what is the “correct” approach to saving and sending data. Let’s say, we receive 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. 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).
Now, when we receive this first JSON, we should think about how we save and send values like id and profile pic.
I know 2 approaches, but I’m not sure, if they are “correct”.
First one is, save everything into arrays as soon as your json arrives. 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. 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
- (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.
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. (So you can identify from where that segue is being called)
I like the second one better. You don’t need to save all the images, just the URLs. The images should be loaded asynchronously (I use EGOImageLoading) and then you can either send the image or the URL to the new view.
You can use the tableView indexPathForSelectedRow to pass data from that row: