UITableView 很慢,即使只有几个对象?

问题描述 投票:0回答:4
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView setRowHeight:100];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
}


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 100) reuseIdentifier:CellIdentifier] autorelease];

        // For Name/Phone: 
        UITextField *name = [[[UITextField alloc] initWithFrame:CGRectMake(75, 22, 200, 25)] autorelease];
        [name setFont:[UIFont systemFontOfSize:14]];
        [name setPlaceholder:@"John Doe"];
        [name setReturnKeyType:UIReturnKeyDone];
        [name setAutocapitalizationType:UITextAutocapitalizationTypeWords];
        [name setAutocorrectionType:UITextAutocorrectionTypeNo];
        [name setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

        UITextField *phone = [[[UITextField alloc] initWithFrame:CGRectMake(75, 67, 200, 25)] autorelease];
        [phone setFont:[UIFont systemFontOfSize:14]];
        [phone setPlaceholder:@"0412 123 123"];
        [phone setReturnKeyType:UIReturnKeyDone];
        //[phone setKeyboardType:UIKeyboardTypePhonePad];
        [phone setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

        UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(9, 11, 302, 89)] autorelease];
        background.image = [UIImage imageNamed:@"book-personaldetailsbg.png"];

        // Add to the View
        [cell addSubview:background];
        [cell addSubview:name];
        [cell addSubview:phone];

        // Add actions:
        [name addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
        [phone addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit]; 

    }

    return cell;
}

这有什么原因吗?我只设置了几个对象,所以我几乎不明白为什么它会滞后。它会跳,而且不是我的手机,因为设置应用程序运行良好。

iphone iphone-sdk-3.0 uitableview
4个回答
10
投票

由于手机渲染每一层的方式,具有许多子视图的 UITableViewCell 在 iPhone 上渲染速度很慢。

阅读此内容:http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/

上述帖子中的信息仍然非常相关且有帮助,但示例项目下载链接已损坏(截至 2011 年 12 月 8 日)。然而,苹果自己的表格视图文档现在已经提供了用于快速滚动日志的平面表格单元格的示例。查看:http://developer.apple.com/library/ios/#samplecode/AdvancedTableViewCells/Introduction/Intro.html

要点是:

  • iPhone 处理 Alpha 的速度不是很快
  • 子视图的
    opaque
    属性应设置为
    YES
  • 如果可能,将子视图绘制到单个不透明视图中以获得最佳性能。

显然,带有需要操作的控件的单元格无法展平,因此我认为您只需尝试确保单元格子视图不透明即可。

我建议的最后一件事是不要在每次请求单元格时分配新对象 - 这肯定是代码中的瓶颈。您应该使用可重用的单元格,并对单元格进行子类化,以便仅在第一次创建时才将其字段分配并添加为子视图。后续调用应使单元出队并使用

prepareForReuse
清除其旧值。


0
投票

我认为你不应该像现在这样设置单元格高度。我相信您应该实现 heightForCellAtIndexPath 方法(可能没有 100% 正确的名称),它会询问您的委托应该使每个单元格有多高。我觉得如果不以“正确”的方式完成,我听说过性能问题。恕我直言

如果我是你,我会尝试的第一件事就是注释掉所有与图像相关的代码,看看表格是否会大幅加速。


0
投票

首先删除该单元格背景。看看这是否有什么不同。


0
投票

在块内添加要加载的图像,就是这样:

dispatch_queue_t 队列 =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(队列, ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:
                                                      [[提供 objectAtIndex:indexPath.row]
                                                       objectForKey: @"url"]]];
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.imagen.image = 图像;
        });

优先使用 Grand Central Dispatch。

© www.soinside.com 2019 - 2024. All rights reserved.