Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cellForRowAtIndexPath not called but numberOfRowsInSection called

I have a simple problem but I can't understand what is. I have a UIViewControler (called RootController) that load a UIView (called SecondView) that contain a tableView. The problem is that the UIView call the numberOfSectionsInTableView and the numberOfRowsInSection but don't call cellForRowAtIndexPath and the table view is not displayed. The code of the RootViewController is:

SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];

And the code of the SecondView is:

@interface SecondView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,retain) UITableView *table;
@end

@implementation SecondView
@synthesize table;

- (id)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
   if (self) {
   self.table = [[UITableView alloc] init];
   self.table.delegate = self;
   self.table.dataSource = self;
   [self addSubview:self.table];
   }
 return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
     }
  cell.textLabel.text = @"Prova";
  return cell;
}

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

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

Can you help me to find the problem? Thank you.

like image 513
Anna565 Avatar asked Jan 29 '14 14:01

Anna565


3 Answers

You need to set the Frame of UITableView

like image 122
Hussain Shabbir Avatar answered Nov 10 '22 05:11

Hussain Shabbir


My problem was that I had a simple class doing the implementing of my delegate and data source, but the lifetime of the simple class was too short.

I was doing

MyDataSourceClass* myClass = [[MyDataSourceClass alloc] initWithNSArray:someArray];
tableView.dataSource = self.tableViewDataSource;
tableView.delegate = self.tableViewDataSource;
[tableView reloadData];

// end of function, myClass goes out of scope, and apparently tableView has a weak reference to it

Needed to be doing

self.tableDataSource = [[MyDataSourceClass alloc] initWithNSArray:someArray];
tableView.dataSource = self.tableDataSource;
tableView.delegate = self.tableDataSource;
[tableView reloadData];
// now at the end of the function, tableDataSource is still alive, and the tableView will be able to query it.

Note that the code above is pseudocode from memory. Take from it the concept of "make sure your data source/delegate lives long", but don't copy paste it, because there's other stuff you need to do (like set your frame etc etc).

like image 13
ArtHare Avatar answered Nov 10 '22 05:11

ArtHare


This could also happen if reloadData is called on a different thread. Make sure it is run on the main thread since all UI stuff has to happen on the main thread.

dispatch_async(dispatch_get_main_queue(),{
            self.myTableView.reloadData()
        });
like image 11
spfursich Avatar answered Nov 10 '22 07:11

spfursich