Get one NSArray

后端 未结 4 1311
夕颜
夕颜 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条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 01:30

    In your UITableViewDataSource methods, combine both arrays and use one or another accordingly:

    - (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;
    }
    

提交回复
热议问题