获取一个NSArray

我想知道如何将两个array合并成一个array

我想组合的tableView显示最近的。

我会根据需要发布任何额外的代码或帮助, 非常感谢!

这个问题的复杂性的原因是数据来自两个不同的来源asynchronous。 饶了我们,我们已经解决了这个问题。 我的build议与其他的不同之处在于,它的目的是立即处理多个asynchronous数据源,在所有其他领域留下更简单的代码。

处理两个asynchronous源的方法是使用嵌套完成序列化它们。 在这里,我只是把你的发布代码分解成两个方法,每个api一个。 每个需要匹配对象pipe理器接口的成功和失败块…

 - (void)loadOneWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure { NSString *apikey = @kCLIENTKEY; NSDictionary *queryParams = @{@"apikey" : apikey}; NSString *path = [NSString stringWithFormat:@"v1/n/?limit=4&leafs=%@&themes=%@", leafAbbreviation, themeID]; [self.eObjectManager getObjectsAtPath:path parameters:queryParams success:success failure:failure]; } - (void)loadTwoWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure { NSString *path = @"v1/u/2/m/recent/?client_id=e999"; [self.iObjectManager getObjectsAtPath:path parameters:nil success:success failure:failure]; } 

现在我们可以使loadMedia做我们所需要的,就是从每个api加载,合并和sorting为一个单一的模型。 声明一个名为combinedModelNSMutableArray属性。 其他答案已经build议这个tableDataList或contentArray。 我build议的关键区别在于将组合作为组合提取的一部分来处理。

 - (void)loadMedia { self.combinedModel = [NSMutableArray array]; [self loadOneWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { [self.combinedModel addObjectsFromArray:mappingResult]; // here's the trick. call API2 here. Doing so will serialize these two requests [self loadTwoWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { [self.combinedModel addObjectsFromArray:mappingResult]; [self sortCombinedModel]; [self.tableView reloadData]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"No?: %@", error); }]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"No?: %@", error); }]; } 

现在只剩下两个问题了(1)对异构对象数组进行sorting,(2)在表视图中渲染异构对象。 第一类:

 - (void)sortCombinedModel { [self.combinedModel sortUsingComparator:^NSComparisonResult(id a, id b) { NSDate *dateA, *dateB; dateA = ([a isKindOfClass:[Feed self]])? ((Feed *)a).published : ((Data *)a).created_time; dateB = ([b isKindOfClass:[Feed self]])? ((Feed *)b).published : ((Data *)b).created_time; return [dateA compare:dateB]; }]; } 

现在对于表, self.combinedModel是表视图的新模型。 所有的数据源方法都应该引用它。 cellForRowAtIndexPath:应该像sorting: 简而言之…

 id model = self.combinedModel[indexPath.row]; if ([model isKindOfClass:[Feed self]) { Feed *feed = (Feed *)model; // configure cell with feed } else { Data *data = (Data *)model; // configure cell with data } 

在你的UITableViewDataSource方法中,结合两个数组并相应地使用一个或另一个:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // API1 + API2 return hArray.count + iArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; if(indexPath.row < hArray.count) { // API 1 YourAPI1Cell *api1Cell = [tableView dequeueReusableCellWithIdentifier:@"YourAPI1Cell"]; // Do everything you need to do with the api1Cell // Use the index in 'indexPath.row' to get the object from you array cell = api1Cell; } else { // API 2 YourAPI2Cell *api2Cell = [tableView dequeueReusableCellWithIdentifier:@"YourAPI2Cell"]; // Do everything you need to do with the api2Cell // Remember to use 'indexPath.row - hArray.count' as the index for getting an object for your second array cell = api2Cell; } return cell; } 

我想你的实际代码工作。 这些是你可以做的步骤。 我没有尝试(不知道它的编译),但你会得到整个想法。

•我会去:

 @property (nonatomic, strong) NSMutableArray *contentArray; 

不要忘记初始化它: contentsArray = [[NSMutableArray alloc] init]; 在做WebService请求之前。

•在方块中,我会做:

 [contentsArray addObjectsFromArray:mappingResult.array]; 

•您需要按date对它们进行sorting,因为我们只需要添加一个方法来对它们进行sorting,但是我不知道FeedData[mappingResult array]中的对象)如何,我会让您这样做。 但是我给你的主要想法,因为在@Wain的答案的评论中,你说的date是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]; }]; 

•在数据源方法tableView:numberOfRowsInSectionreturn [contentsArray count];

•在数据源方法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 } 

我会保留表格数据的一个属性:

 @property (strong, nonatomic) NSMutableArray *tableDataList; 

并在每个成功块中调用一个方法:

 - (void)addTableData:(NSArray *)items { [self.tableDataList addObjectsFromArray:items]; [self.tableDataList sortUsingDescriptors:...]; [self.tableView reloadData]; } 

mappingResult.array传递给它,并且填写sorting描述符信息以获得时间轴sorting。

那么你的表视图的委托/数据源方法是非常简单的,只需引用self.tableDataList

这与@Larme的答案相似。 从这里,我会得到的项目,并检查类来决定如何configuration单元格:

 id item = [self.tableDataList objectAtIndex:indexPath.row]; if ([item isKindOfClass:[Feed class]]) { Feed *feed = (Feed *)item; ... // configure the feed cell } else { Data *data = (Data *)item; ... // configure the data cell }