I have a TableView that I am trying to load data into. From the NSLog displays the data is correct and present. However the following method isn’t being called, e.g. the NSLog comments are not appearing at all.:
- (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];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
NSString *cellValue = [listOfStates objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
NSLog(@"Passed: cellForRowAtIndexPath");
}
Similarly the init isn’t being run either. I though init was always run, if present?
- (id)init {
if ((self = [super init])) {
listOfStates = [[NSMutableArray alloc] init];
stateName = [[NSString alloc] init];
NSLog(@"INIT StateName %@", stateName);
}
return self;
NSLog(@"Passed: init");
}
Any ideas?
tableView:cellForRowAtIndexPath:is called by the table view on its data source when a cell is needed, this can be either when a cell is first shown, or when the user is scrolling and the cell will appearyour init method is not being called because UITableView uses
initWithFrame:style:and UITableViewController (it wasnt clear which init you are trying to override) usesinitWithStyle:, not the “plain” init method. If you want to do custom setup you should override these methodsand as the other answers have mentioned, anything you put after a return statement will never be executed