0

I have a UITableView with custom cells in a iPad app. When I sort the array that is the data source for that UITableView, and I click on a row in the UITableView, the detail data for that row is wrong (I empty the selected row data into UITextFields)! If I don't sort the array, the detail is correct! Here is the code that sorts the datasource array:

-(void) reloadClientList: (BOOL *)dontDeleteKey  {

    custDataArray = [NSMutableArray new];  //Init Array to hold TableView Data

    if(boSortClientList.selectedSegmentIndex == 0)  {  //  sort alphabetically
        [custDataArray addObjectsFromArray: [ClientInfo MR_findAllSortedBy:@"aClientLastName" ascending:YES]];
    }
    else if(boSortClientList.selectedSegmentIndex == 1)  {  //  sort by phone number
        [custDataArray addObjectsFromArray: [ClientInfo MR_findAllSortedBy:@"aClientPrimaryPhone" ascending:YES]];
    }
    else  {  //  no sort
        [custDataArray addObjectsFromArray:[ClientInfo MR_findAll]];
    }

    [self.clientList reloadData];

    if(dontDeleteKey == false)
        selectedClientKey = nil;  // clear selectedClientKey
}

Why is this occurring? What do I need to do to fix it?

1
  • 2
    show code for cellForRowAtIndexPath and didselectRowAtIndexPath
    – Dan F
    Apr 15, 2013 at 18:10

3 Answers 3

4

Most likely your sorting the data in an array and using the sorted array in cellForRowAtIndexPath:

Then in didSelectRowAtIndexPath you are using the unsorted array to get which detail page to go to. You should be able to fix your issue by using the sorted array in didSelectRowAtIndexPath.

1
  • Fantastic! That was exactly the problem... thank you so much; I really appreciate it. Apr 15, 2013 at 18:20
1

Every time you select a row, this code in didSelectRowAtIndexPath recreates the array once again:

custDataArray = [NSMutableArray new];
[custDataArray addObjectsFromArray:[ClientInfo MR_findAll]];

You need to perform a check whether or not your custDataArray is nil. If it's not nil then you don't need those lines.

1

Create an ivar to hold your NSMutableArray. You're sorting your array reloadClientList, but you're not using the same array in your didSelectRowAtIndexPath.

Not the answer you're looking for? Browse other questions tagged or ask your own question.