I have a UITableView and the data is pulled from a database stored externally. Obviously it takes some time to fetch that data, do when the app is launched there is no data in the array which the table uses for it's data.
When the data is loaded from the external source I call [self.tableview reloadData];
but there is a slight problem. The first cell doesn't have any text in it until after it is redrawn, either by selecting it or by scrolling it off the screen. I've tried adding a call to [self.tableView setNeedsLayout];
and [self.tableView setNeedsDisplay]
but this has no apparent effect.
The array contains the correct data at the time of reloading the table so it's not a race condition thing (I believe!).
Any other ideas about what could be causing this?
@implementation EXPMasterViewController
@synthesize detailViewController = _detailViewController;
@synthesize partiesModel = _partiesModel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Master", @"Master");
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
self.partiesModel = [[Parties alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadData)
name:@"ReloadData"
object:nil];
}
return self;
}
-(void)reloadData{
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView setNeedsDisplay];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.partiesModel.partiesArray) return [self.partiesModel.partiesArray count];
else return 1;
}
- (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] autorelease];
}
// Configure the cell.
cell.textLabel.text = [[self.partiesModel.partiesArray objectAtIndex:indexPath.row] name];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[[EXPDetailViewController alloc] initWithNibName:@"EXPDetailViewController" bundle:nil] autorelease];
}
self.detailViewController.detailItem = [self.partiesModel.partiesArray objectAtIndex: indexPath.row];
}
Ah fixed it, it was something to do with initially selecting a row. Removing [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
cleared it all up