简体   繁体   中英

NSMutableArray: How add a sorting value (int) to array's objects AFTER sorting it?

Got a tableView with some player objects in it, sorted ASC by best playerTime after each game try a player makes.

Now I want to add a value (int) that shows the players placement in the playerlist (tableview), that will be reflected on the the players custom cell in the tableView. Got the custom cell working already with the dynamic data, just I need the "placement" int.

How do I handle the array to do this? Must be done after the list (NSArray) been sorted by best playerTime.

  • I guess, some kind of iteration step one and two maybe (like an if-then statement)?
  • Maybe there is a smart NSArray property for this specific need?

Any tips or suggestions? Thank's.

Just display the row's value into your custom cell's label:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //...
    myCustomCell.positionLabel.text = [NSString stringWithFormat:@"%i", (indexPath.row + 1)];
    //...
}

Now if you have a player object and want to retrieve its position in your sorted array, you just do this:

int playerPosition = [mySortedArray indexOfObject:playerObject] + 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM