Search code examples
iosarraysuitableviewrestkit

Displaying Text Label from a combined Array (using APIs)


I have a combined array, that is combining the news headline results of API1 and API2. How do I get the "headline text" from each object in the new combined array?

Right now I have the app working fine when I'm only using one of the API's at a time. But now that I'm putting the objects in a combined array, I can't figure out how to display the cell.headline.text since the sources I'm pulling from aren't from the same exact JSON anymore (i.e. could be pulling from API1 or could be pulling from API2).

I'm using RestKit.

WebListViewController.m (Only using API1)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebListCell"];

    F *fLocal = [hArray objectAtIndex:indexPath.row];
    NSString *headlineText = [NSString stringWithFormat:@"%@", fLocal.headline];
    cell.headlineLabel.text = headlineText;
}

WebListViewController.m (Only using API2)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebListCell"];

    D *dLocal = [iArray objectAtIndex:indexPath.row];
    NSString *otherHeadlineText = 
    [NSString stringWithFormat:@"%@", dLocal.head.headline];
    cell.headlineLabel.text = otherHeadlineText;
}

WebListViewController.m (Trying to Combine API1 + API2)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is where I need help.  Don't know if this line below even makes sense?
    WebListViewController *combinedArray = [array objectAtIndex:indexPath.row];
}

Solution

  • You don't say anything about your RestKit usage, and using core data would probably be a great idea for you. You should also think about mapping all of the objects into the same destination object / entity.

    Anyway, you can merge your arrays to get a list of just the headline texts with:

    combinedArray = [hArray valueForKey:@"headline"];
    combinedArray = [combinedArray arrayByAddingObjectsFromArray:[iArray valueForKey:@"head.headline"]];
    

    (can't test right now, second line may require valueForKeyPath:)