asj*_*sjj 4 uidatepicker uitableview ios
我有一个动态的tableview,其中选择了一个单元格并显示另一个包含UIDatePicker在其下方的单元格- 就像在日历应用程序中一样.
这非常有效,但是当用户首次滚动它时(当它的高度为0时),我遇到了单元的初始加载问题.
调查为什么它很慢我dequeueReusableCellWithIdentifier在NSLogs中包含调用来查看时间.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
NSLog显示了这个:
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
Run Code Online (Sandbox Code Playgroud)
日期/时间是包含2个UILabel的单元格,DatePicker是包含UIDatePicker的单元格.
UIDatePicker单元格需要大约半秒钟才能完成.在第一次滚动之后,它加载得更快,因此没有明显的延迟 - 除了第一次点击日期单元格,因此它下面的DatePicker单元格将其高度从0更改为220.
NSLog首次打开单元格:
14:59:34.334 Creating cell:DatePicker
14:59:34.877 Finished cell:DatePicker
Run Code Online (Sandbox Code Playgroud)
我注意到日历应用程序在第一次打开UIDatePicker单元格时有一点延迟,但这似乎并不像我的应用程序那样长.
我正在测试iPhone 4,这可能是一个促成因素.
有没有人对我可能做错了什么或能做些什么来加快速度?
我通过从单元格中删除UIDatePicker来解决这个问题,以编程方式创建UIDatePicker并将其作为子视图添加到cellForRowAtIndexPath中的单元格contentView.
我在viewDidLoad中初始化了一个datePicker和timePicker.
- (void)viewDidLoad
{
[super viewDidLoad];
datePicker = [[UIDatePicker alloc] init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
timePicker = [[UIDatePicker alloc] init];
[timePicker setDatePickerMode:UIDatePickerModeTime];
}
Run Code Online (Sandbox Code Playgroud)
然后在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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1406 次 |
| 最近记录: |