Crash while trying to move UITableView rows

I have some pretty complicated rules for moving lines around UITableView. There is an undefined number of sections and lines in each section and based on different rules, lines can be moved inside or between user sections in certain other places.

All data updates and everything works. But sometimes, after moving the line, the application will be a wig, and suddenly an empty place appears where the line should be displayed.

I use:

              - (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath

to indicate where the user is allowed to drag rows based on where the cell is located. 98% of the time it works. But in some cases, when the user is allowed to move between sections (it is impossible to change the order of the lines in the section), this error appears, after which the application is reset after scrolling through the area without a line.

The exception thrown is pretty useless:

Application termination due to an uncaught exception "NSRangeException", reason: "*** - [NSCFArray objectAtIndex:]: index (6) outside (6)

None of my codes are on the stack. The last method specific to UITableView is

-[UITableView(UITableViewInternal) _visibleCellForGlobalRow:]

Has anyone seen this problem before? Any ideas?

+5
source share
7

. , , - . , , . , .

0

, , , .

, . . , , .

, 1 2 , 1, 2, 2, tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: , , . , , .

, , ( 2), ! tableView:cellForRowAtIndexPath: . - , .

[tableView reloadData] tableView:moveRowAtIndexPath:toIndexPath:. , . : reloadData tableView:moveRowAtIndexPath:toIndexPath: , . , , no-op.

, :

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)pathSrc toIndexPath:(NSIndexPath *)pathDst
{
  // APPLE_BUG: after doing the delayed table reload (see below), we get a bogus
  // request to move a nonexistant cell to its current location
  if (pathSrc.row == pathDst.row && pathSrc.section == pathDst.section)
    return;

  // update your data model to reflect the move...

  // APPLE_BUG: if you move a cell to a row that off-screen (because the destination
  // has been modified), the bogus cell gets created and eventually will cause a crash
  [self performSelector:@selector(delayedReloadData:) withObject:tableView afterDelay:0];
}

- (void)delayedReloadData:(UITableView *)tableView
{
  Assert(tableView == self.tableView);
  [tableView reloadData];
}

, UI. . , , , .

. , , , ( ) . , , .

+2

, - - 6 , 0-5 ( 6 ).

, :

NSUInteger index = [somearray count];
id obj = [somearray objectAtIndex:index];

count - , 0, count - 1.

, - , .

+1

, - , [tableView beginUpdates] [tableView endUpdates] , . , , , ( ).

+1

targetIndexPathForMoveFromRowAtIndexPath

DataSource: tableView:moveRowAtIndexPath:toIndexPath:?

iPhone OS, :

Tableview: moveRowAtIndexPath: toIndexPath: ( ). , .

:

, , Tableview: targetIndexPathForMoveFromRowAtIndexPath: toProposedIndexPath: ( ). .

tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: , , tableView:moveRowAtIndexPath:toIndexPath:

, , , .

+1

Cityarray ...

   id obj =[Cityarray objectatindex:sourceindexpath.row];
        [Cityarray removeObjectatindex:(sourceindexpath.row)];
        [Cityarray insertObject:obj atindex:destinationindexpath.row];
+1

. , , iOS 3.0, , , . iOS 4.0 .

, iOS 3.0 , .

0

All Articles