Reputation: 543
I have a dynamic tableview where a cell is selected and shows another cell containing a UIDatePicker
below it - like in the calendar app.
This works really well but I am having trouble with the initial load of the cell when it is first scrolled to by the user (when it has a height of 0).
Investigating why it was slow I wrapped the dequeueReusableCellWithIdentifier
call in NSLogs to look at timings.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [[cellIdentifiers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
NSLog(@"Creating cell:%@", CellIdentifier);
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(@"Finished cell:%@", CellIdentifier);
return cell;
}
The NSLog shows this:
14:47:42.427 Creating cell:Date
14:47:42.437 Finished cell:Date
14:47:42.470 Creating cell:DatePicker
14:47:43.006 Finished cell:DatePicker
14:47:43.046 Creating cell:Time
14:47:43.055 Finished cell:Time
14:47:44.753 Creating cell:DatePicker
14:47:45.253 Finished cell:DatePicker
Date/Time are cells containing 2 UILabels and DatePicker is the cell containing a UIDatePicker.
The UIDatePicker cell takes around half a second to complete. After the first time scrolling it loads much faster so there is no noticeable lag - except for the first time the date cell is tapped and so the DatePicker cell below it changes it's height from 0 to 220.
NSLog for first opening the cell:
14:59:34.334 Creating cell:DatePicker
14:59:34.877 Finished cell:DatePicker
I've noticed the calendar app has a slight delay when opening the UIDatePicker cell for the first time but this doesn't seem to be as long as it takes in my app.
I'm testing on an iPhone 4 which may be a contributing factor.
Does anyone have any ideas on what I could be doing wrong or could do to make this faster?
Upvotes: 4
Views: 1457
Reputation: 543
I have solved this by removing the UIDatePicker from the cell, creating a UIDatePicker programmatically and adding it as a subview to the cells contentView in cellForRowAtIndexPath.
I initialise a datePicker and timePicker in viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
datePicker = [[UIDatePicker alloc] init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
timePicker = [[UIDatePicker alloc] init];
[timePicker setDatePickerMode:UIDatePickerModeTime];
}
Then in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = [[cellIdentifiers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if ([CellIdentifier isEqualToString:@"DatePicker"] && datePickerOpen)
{
[cell.contentView addSubview:datePicker];
}
else if ([CellIdentifier isEqualToString:@"TimePicker"] && timePickerOpen)
{
[cell.contentView addSubview:timePicker];
}
return cell;
}
Upvotes: 5