Application Crashing On UITableView InsertRows

JSpringJSpring GBUniversity ✭✭

My application loads a set number of rows in the table dependent on the available real estate and I am trying to load in more as the user scrolls past the last row. At the moment I have the following:

public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
    var cell = tableView.DequeueReusableCell (CellIndentifer) as DashboardTableCell ??
               new UITableViewCell (UITableViewCellStyle.Default, CellIndentifer) as DashboardTableCell;
    cell.SetupCell (dataLoaded, indexPath); //Could do this async with an activity indicator there until load

    if (indexPath.Row > 0 && (indexPath.Row % (Globals.maxLoadRowsOfData-1) == 0))
        this.launchReload (tableView, indexPath);

    Console.WriteLine ($"Currently At Row:{indexPath.Row} Section:{indexPath.Section}");
    return cell;    
}

public void launchReload(UITableView tableView, NSIndexPath indexPath)
{
    // We know that the indexPath passed in contains data, so we are inserting at the row after.
    var newIndex = indexPath.Row + 1;
    if (myDataList.Count < newIndex)
        return; // If there is less data than this point then back up!

    for(int i=newIndex; i < newIndex + Globals.maxLoadRowsOfData; i++)
    {
        NSIndexPath newIndexPath = NSIndexPath.FromRowSection (newIndex, indexPath.Section);
        try {
            tableView.InsertRows (new NSIndexPath[]{newIndexPath}, withRowAnimation: UITableViewRowAnimation.Automatic);        
        } catch (Exception ex) {
            Console.WriteLine ($"Exception Caught Inserting Row Into Table\n--\n{ex}");
        }

    }
}

On the simulator the maxLoadRowsOfData is 10, therefore after the 9th row I want to load some more data, which is being fired (the launchReload method). However after the tableView.InsertRows is executed I promptly get the following - and the exception is not being caught so I can't get an exception message (if someone could assist in catching this so I could get the message which would be more helpful!):

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:1720

And the stack trace from the Crash Report:

0   libsystem_kernel.dylib          0x0968f58a __pthread_kill + 10
1   libsystem_pthread.dylib         0x09657448 pthread_kill + 101
2   libsystem_c.dylib               0x093ff65d abort + 156
3   co.intellidrive.tracker         0x0003bcb9 uncaught_exception_handler + 38
4   com.apple.CoreFoundation        0x08650e4e __handleUncaughtException + 830
5   libobjc.A.dylib                 0x089bb0ae _objc_terminate() + 100
6   libc++abi.dylib                 0x08f64d0d std::__terminate(void (*)()) + 14
7   libc++abi.dylib                 0x08f6472b __cxa_throw + 122
8   libobjc.A.dylib                 0x089baf23 objc_exception_throw + 339
9   com.apple.CoreFoundation        0x086508aa +[NSException raise:format:arguments:] + 138
10  com.apple.Foundation            0x00cdfd26 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
11  com.apple.UIKit                 0x0157ae2a -[UITableView _endCellAnimationsWithContext:] + 17170
12  com.apple.UIKit                 0x01593ed6 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 363
13  com.apple.UIKit                 0x01593f26 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 56

Any assistance would be great - at the very least a way to catch the exception to see the message! At 10 in the stackTrace there is a description being passed back and would like to see what it is.

Thanks!

Tagged:

Posts

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    Haven't tried it but here's what I would do:

    • Override WillDisplayCell() in the delegate. In there, put a check similar to what you have now in GetCell() to see if you have reached the end.
    • If end has been reached, update your data model (meaning: add new rows to your List<T> or whatever you are using.
    • Because your table source gets the number of rows from your model it should automatically update now.
    • If it does not, try calling ReloadData(), or call table view's BeginUdpates(), then a use the calls to InsertRow() and call EndUpdates().

    It is important that your model is updated first. The UITableView just displays what is in there.

    About the exception: are you sure it is happening where you are trying to catch it?

Sign In or Register to comment.