简体   繁体   中英

Crash in UITableViewController after numberOfSectionsInTableView

I have subclassed an UITableViewController , which I am instantiating trough a segue.

It seems that the table view controller is correctly instantiated as viewDidLoad and numberOfSectionsInTableView are being called. In fact, numberOfSectionsInTableView is called even twice . When it is called first, it has a valid value for tableView , when it is called for the second time (right after the first call), tableView is nil .

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [super numberOfSectionsInTableView:tableView] + 1 ;
}

I am using a static cell (in section 0, row 0) which I have set up in Interface Builder and I am adding a dynamic number of rows in the second section (section 1). That is why I have added 1 to [super numberOfSectionsInTableView:tableView] . There is no custom code in the parent's tableView class, everything is just set up in Interface Builder.

All outlets seem to be set correctly:

在此处输入图片说明

When the execution of the code continues, the app crashes with the error message:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]

Do you have any idea on what could cause the error?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [super numberOfSectionsInTableView:tableView] + 1 ;
}

Basically means that every time numberOfSectionsInTableView gets called the number of sections in the table will increase by 1. That being said, if you are only ever going to have two sections in the table, you'd be better off with return 2; .

Now, on to your error. It's kind of hard to say considering I don't know the exact context or where the error occurred, but my first guess is that the crash happened in cellForRowAtIndexPath because your datasource is either nil or and is an empty array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM