In my header file:
@interface HTMLClassesViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
So I did declare the dataSource and delegate.
In my implementation file:
- (void)viewDidLoad {
[super viewDidLoad];
if (self.arrayOfClasses == nil) {
self.arrayOfClasses = [[NSMutableArray alloc] init];
}
NSMutableArray *array = [[NSMutableArray alloc] init];
... // Gather data from HTML source and parse it into "array"
self.arrayOfClasses = array; //arrayOfClasses here is non-nil (with correct objects)
NSLog(@"%@ test1", [self.arrayOfClasses objectAtIndex:0]);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"%@ test1.5", [self.arrayOfClasses objectAtIndex:0]);
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%@ test2", [self.arrayOfClasses objectAtIndex:0]);
return [self.arrayOfClasses count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@ test3", [self.arrayOfClasses objectAtIndex:0]);
static NSString *CellIdentifier = @"WebCollegeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.arrayOfClasses objectAtIndex:indexPath.row];
return cell;
}
And this is the output from NSLog:
2012-11-24 22:52:19.125 ArizonaCollegeSearch[72404:c07] CSE 240 test1
2012-11-24 22:52:19.126 ArizonaCollegeSearch[72404:c07] CSE 240 test1.5
2012-11-24 22:52:19.127 ArizonaCollegeSearch[72404:c07] (null) test1.5
2012-11-24 22:52:19.127 ArizonaCollegeSearch[72404:c07] (null) test2
As you can see, numberOfSectionsInTableView is being called with an non-null object, and then with a null object, and numberOfRowsInSection is being called with a null object, and cellForRowAtIndexPath is not being called at all. I don’t even have a [self.tableView reloadData] anywhere.
Any suggestions?
So the property holding your array needs to be declared
strong, else it will get deallocated when the variable it has been assigned to is deallocated. And in the case of a local variable (yourNSMutableArray *array), that’s the end of its scope, e.g. when the functionviewDidLoadreturns.