This is my code to populate UITableView, very very simple:
file .h
@interface ViewController: UIViewController {
IBOutlet UITableView *table;
NSMutableArray *array1;
NSString *string1
}
file .m
- (void)viewDidLoad
{
[super viewDidLoad];
array1 = [[NSMutableArray alloc] initWithObjects: @"1", @"2", @"3", @"4", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array1.count; // <------- here there is the problem with iOS 5.1, also [array1 count]
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:@" %@",[array1 objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont systemFontOfSize:13];
cell.textLabel.textColor = [UIColor blackColor];
return cell;
}
Well, in iOS 6 (simulator and real device) its all right, UITableView loads and shows all rows of array1 objects; in iOS 5.1 (simulator and real device) it doesnt crash but UITableView is empty, no rows showed (Header and Footer are attached), and I’have noticed this could be a problem with the numberOfRowsInSection: array1.count method, I think in iOS 5.1 it doesnt recognize the right number of rows. Please, what’s the best way to write a code compatible with iOS 5.1 and iOS6?
After populating the array1 do
Since I think the data is not available when the tableview is loaded. And this could be different between iOS 5 and 6.