Get one NSArray

后端 未结 4 1428
夕颜
夕颜 2021-01-24 00:48

I was wondering how to combine two array\'s into one array.

I want the combined tableView to show the most recent

4条回答
  •  萌比男神i
    2021-01-24 01:27

    I suppose that your actual code works. These are steps you could do. I didn't try (don't know if its compiles), but you'll get the whole idea.

    • I'll go with:

    @property (nonatomic, strong) NSMutableArray *contentArray;
    

    Don't forget to initialize it: contentsArray = [[NSMutableArray alloc] init]; before doing the WebService requests.

    • In the blocks, I'll do:

    [contentsArray addObjectsFromArray:mappingResult.array];
    

    • You need to sort them by dates, so since we just need to add a method to sort them, but I don't know how are Feed & Data (objects in the [mappingResult array]), I'll let you do it. But I give you the main idea, since in the comments of the answer of @Wain, you said that the dates were NSString.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //Do the dateFormatter settings, you may have to use 2 NSDateFormatters if the format is different for Data & Feed
    //The initialization of the dateFormatter is done before the block, because its alloc/init take some time, and you may have to declare it with "__block"
    //Since in your edit you do that and it seems it's the same format, just do @property (nonatomic, strong) NSDateFormatter dateFormatter;
    NSArray *sortedArray = [contentsArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) 
    {
        NSDate *aDate, bDate;
        if ([a isKindOfClass:[Feed class]])
            aDate = [dateFormatter dateFromString:(Feed *)a.created_time];
        else //if ([a isKindOfClass:[Data class]])
            aDate = [dateFormatter dateFromString:(Data *)a.published];
       if ([b isKindOfClass:[Feed class]])
            bDate = [dateFormatter dateFromString:(Feed *)b.created_time];
        else //if ([b isKindOfClass:[Data class]])
            bDate = [dateFormatter dateFromString:(Data *)b.published];
        return [aDate compare:bDate];
    }];
    

    • In the datasource method tableView:numberOfRowsInSection, do return [contentsArray count];

    • In the datasource method tableView:cellForRowAtIndexPath:

    if ([[[contentsArray objectAtIndexPath:[indexPath row]] isKindOfClass:[Feed class]])
    {
        Feed *feed = [contentsArray objectAtIndexPath:[indexPath row]];
        CellClassFeed *cell = [tableView dequeueReusableCellWithIdentifier:@"APICell1"];
        //Do your thing
    }
    else //if ([[[contentsArray objectAtIndexPath:[indexPath row]] isKindOfClass:[Data class]])
    {
        Data *data = [contentsArray objectAtIndexPath:[indexPath row]];
        CellClassData *cell = [tableView dequeueReusableCellWithIdentifier:@"APICell2"];
        //Do your thing
    }
    

提交回复
热议问题