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:
我在iPad应用程序中有一个带有自定义单元格的UITableView。当我对作为该UITableView的数据源的数组进行排序时,我单击UITableView中的一行时,该行的详细数据是错误的(我将所选行数据清空到UITextFields中)!如果我不对数组进行排序,细节是正确的!以下是对数据源数组进行排序的代码:
-(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?
为什么会这样?我需要做些什么来解决它?
3 个解决方案
#1
4
Most likely your sorting the data in an array and using the sorted array in cellForRowAtIndexPath:
最有可能的是您在数组中对数据进行排序并使用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
.
然后在didSelectRowAtIndexPath中,您使用未排序的数组来获取要转到的详细信息页面。您应该能够通过在didSelectRowAtIndexPath中使用已排序的数组来解决您的问题。
#2
1
Every time you select a row, this code in didSelectRowAtIndexPath
recreates the array once again:
每次选择行时,didSelectRowAtIndexPath中的此代码将再次重新创建数组:
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.
您需要检查您的custDataArray是否为零。如果它不是零,那么你不需要那些线。
#3
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
.
创建一个ivar来保存您的NSMutableArray。您正在对数组reloadClientList进行排序,但您没有在didSelectRowAtIndexPath中使用相同的数组。
#1
4
Most likely your sorting the data in an array and using the sorted array in cellForRowAtIndexPath:
最有可能的是您在数组中对数据进行排序并使用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
.
然后在didSelectRowAtIndexPath中,您使用未排序的数组来获取要转到的详细信息页面。您应该能够通过在didSelectRowAtIndexPath中使用已排序的数组来解决您的问题。
#2
1
Every time you select a row, this code in didSelectRowAtIndexPath
recreates the array once again:
每次选择行时,didSelectRowAtIndexPath中的此代码将再次重新创建数组:
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.
您需要检查您的custDataArray是否为零。如果它不是零,那么你不需要那些线。
#3
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
.
创建一个ivar来保存您的NSMutableArray。您正在对数组reloadClientList进行排序,但您没有在didSelectRowAtIndexPath中使用相同的数组。