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

前端 未结 7 1882
無奈伤痛
無奈伤痛 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: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 
    

    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.

提交回复
热议问题