Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cellForRowAtIndexPath never called

I have a UITableView as a subview of a UIViewController. The viewController UI is managed through a storyboard. The delegate and datasource are set to the viewController as required.

I have the following code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (updateType == Profile) {
        return 15;
    }
    else {
        return 6;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

The above two methods are called exactly as they should. Calling [self.tableview reloadData] results in the methods being called again. The baffling part is that - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is never called. Ever.

The datasource and delegate are set in code where self.tableview is an IBOutlet to the UITableView in the storyboard:

self.tableview.delegate = self;
self.tableview.dataSource = self;

Answer Found

I am using storyboards with auto layout. The issue was that the dimensions were set to wRegular hRegular despite the rest of the storyboard been set to wAny hAny. Simply switching this has made it work! Ridiculous I know but it is an answer nonetheless.

like image 572
JordanMazurke Avatar asked Jun 20 '15 21:06

JordanMazurke


2 Answers

I am using storyboards with auto layout. The issue was that the dimensions were set to wRegular hRegular despite the rest of the storyboard been set to wAny hAny. Simply switching this has made it work! Ridiculous I know but it is an answer nonetheless!

This works:
This works

This does not: This doesnt

This has driven me crazy for far too long and I still do not fully understand the mechanics as to why this works but it does.

like image 155
JordanMazurke Avatar answered Nov 10 '22 05:11

JordanMazurke


There are a few situations that delegate methods are not called. Therefore, check your setup first through debugging tool to see whether your setup is appropriate like checking whether existence of table view, delegate and dataSource in memory. If your data source is empty, the table view will not even try to call which is not your case since you do return certain number.

Normally, it's just a small mistakes that programmers made based on what I made and saw before. So, double and triple check your code. And you better post your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath as well.

UPDATE FROM WRITER's ANSWER: It turns out that the table view is not visible under some size class. That's why iOS will not even try because it's not visible at all. This is not a bug from Apple and it can be detected by view debugging.

like image 1
Lucas Huang Avatar answered Nov 10 '22 05:11

Lucas Huang