“Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error

前端 未结 7 1891
無奈伤痛
無奈伤痛 2021-02-13 16:24

I have an app that is selecting a person from their contacts list and takes their First name, last name and email. It then saves the first name to a nsmutablearray and puts it i

相关标签:
7条回答
  • 2021-02-13 16:42

    I know it's stupid - I got the same error because I forgot to set the delegate and dataSource.

    So after inserting rows and doing tableView.endUpdates() the tableView thought it must have some rows - but due to the unlinked dataSource, it has not.

    0 讨论(0)
  • 2021-02-13 16:46

    The comments above about implementing -tableView:numberOfRowsInSection: are correct. The assertion is checking the returned value expecting it will increase.

    However since you didn't set your dataSource on the UITableView its calling (or not calling) a method that doesn't exist and getting 0.

    You need to set the myTableView.dataSource and (since you also implement the delegate protocol) myTableView.delegate to self.

    You're also likely to need something like

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        ...
    }
    

    Unless you're registering that somewhere else or your storyboard has a "Prototype Cell" with the identifier "Cell" which your code asks for.

    0 讨论(0)
  • 2021-02-13 16:47

    You need to maintain count for contacts array and increment accordingly.
    and while creating indexPath you need to set appropriate indexPathForRow: and section count(if required).

    - (void)displayPerson:(ABRecordRef)person
    {
    
      ..........
    
    [contacts insertObject:firstName atIndex:0];
    
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // you cant do this increment appropriately
    
     .........
    }
    
    0 讨论(0)
  • 2021-02-13 16:48

    I find this problem commonly occurs when I am placing a table view inside of a View Controller. If you're using a UITableViewController jump to 3.

    These steps may help:

    1: In your View Controller .h file make sure you add the following:

    @interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
    

    2: Next create an IBOutlet for your table view by ctrl + drag to your .h class. It should look like:

    @property (weak, nonatomic) IBOutlet UITableView *tableView;
    

    3: Next step is to ctrl + drag to your View Controllers icon (see image)

    enter image description here

    You need to do this twice selecting:

    - delegate
    - datasource
    

    Finally, in your .m file, you should have the following method:

    - (void) viewWillAppear:(BOOL)animated{
        //[self.tableView beginUpdates];
        //[self.tableView endUpdates];
    
        [self.tableView reloadData];
    }
    

    You can use either beginUpDates/endUpdates or reloadData, however Apple docs recommend reloadData.

    Once done your table should work fine.

    0 讨论(0)
  • 2021-02-13 16:56

    I have encountered same problem as this. All you have to do is change

    [self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];`
    

    to

     [self.myTableView beginUpdates];
    
     [self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
    
     [self.myTableView endUpdates];
    

    From UITableView Documentation

    beginUpdates

    Begin a series of method calls that insert, delete, or select rows and sections of the receiver.

    when you use beginUpdates, you must call endUpdates and not reloadData.

    You can check https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html for more UITableView information.

    Hope this helps!

    0 讨论(0)
  • 2021-02-13 16:57

    Please check your tableview datasource and delegate methods. You may be passing empty array to datasource methods. And don't forget to reload tableview after getting data.

    0 讨论(0)
提交回复
热议问题