objective-cuitableviewnsmutablearrayxcode4.2

inverting the list of items in a UITableView


I populate a NSMUtableArray list of items in a UITableView .

I was wondering if there is a function that I can use to invert order of the displayed list ?

I know that I can probably just create a new list with an inverted for loop but that s somehow a waste of memory

Thank you


Solution

  • Why not just invert the order you fetch the data inside the datasource method?

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      NSUInteger dataItemIndex = self.inverted ? (self.dataItems.count - 1 - indexPath.row) : indexPath.row;
    
      // Fetch item at index and return cell ...
    }
    

    I'm afraid there is no built in method to invert the object order of an array. This question may also be of help.