Reputation: 117
currently I have been trying to write a UITableView parent controller that monitors the current state of it's tableView. This tableView has 2 sections, and my plan is to have an NSMutableArray
for each section that would mirror the current layout for saving to a NSUserDefaults page later. It is displayed in editing mode with flags that would allow the user to use Apple's default drag implementation
I have had constant crashes with cross-section dragging for unknown reasons with my current - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
I have no problem keeping my arrays organized while one row is moved within the same section, but I have not been able to solve this issue even when trying to manually refresh my rows . All the cell.reuseIdentifier's are supplied by me & unique so I am confident that there has not been any sort of compiler misinterpretation
Here is how the method looks
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
if (sourceIndexPath != destinationIndexPath) { //not in the same spot
//same section, different row only, works without issues
if (sourceIndexPath.section == destinationIndexPath.section) {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:sourceIndexPath];
if (destinationIndexPath.section == 0) {
[self.visibleSections removeObject:cell.reuseIdentifier];
[self.visibleSections insertObject:cell.reuseIdentifier atIndex:(int)destinationIndexPath.row];
}
else if (destinationIndexPath.section == 1) {
[self.hiddenSections removeObject:cell.reuseIdentifier];
[self.hiddenSections insertObject:cell.reuseIdentifier atIndex:(int)destinationIndexPath.row];
}
}
//moving to different section
else {
NSString * id = [tableView cellForRowAtIndexPath:sourceIndexPath].reuseIdentifier;
if (id.length != 0) {
//movedf from visible to hidden
if (sourceIndexPath.section == 0) {
[self.visibleSections removeObject:id];
[self.hiddenSections insertObject:id atIndex:(int)destinationIndexPath.row]; //CRASHING HERE
}
//moved from hidden to visible
else {
[self.hiddenSections removeObject:id];
[self.visibleSections insertObject:id atIndex:(int)destinationIndexPath.row]; //CRASHING HERE
}
}
}
}
From logging every line, I have determined the crash usually seems to be on the -(void)insertObject:atIndex:
, in some cases I have tried puttng [tableView begin/endUpdates]
around and it would work for one drag between sections but always crashing on a subsequent drag, but it also resulted in more than a few UI bugs with invisble rows which makes me think it isn't the route to go. I tried executed my array reordering asynchronously but that was an immediate crash as well
Ultimately I want to be able to get a representation of the current order of the tableView so I can save that to my NSUserDefaults for maintaining the user's desired order in future use, and would appreciate if anyone has a solution to this issue / a better way to keep track of the state. Thanks
Upvotes: 0
Views: 159
Reputation: 1741
I'm not sure I understand the code and I'm missing the hiddenSections/visibleSections definition but I think you're missing something (and I apologize if I misunderstood).
NSString *temp = [self.visibleSections removeObject:id];
[self.hiddenSections insertObject:temp atIndex:(int) destinationIndexPath.row];
Upvotes: 1