【问题标题】:Slow Scrolling UITableView?UITableView 滚动慢?
【发布时间】:2013-05-22 16:08:14
【问题描述】:

我的 UITableView 在消息(内容)加载到单元格后,滚动时会出现非常明显的延迟,有时会冻结几秒钟。这很奇怪,因为一旦用户滚动就会加载所有消息。关于如何使这种快速滚动没有问题的任何想法?

谢谢!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"MailCell";

    MailCell *cell = (MailCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MailCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        // Anything that should be the same on EACH cell should be here.

        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor colorWithRed:40.0/255.0 green:148.0/255.0 blue:196.0/255.0 alpha:1];
        cell.selectedBackgroundView = myBackView;

        cell.messageText.textAlignment = NSTextAlignmentLeft;
        cell.messageText.lineBreakMode = NSLineBreakByTruncatingTail;
    }

    NSUInteger row = [indexPath row];

    // Extract Data

    // Use the message object instead of the multiple arrays.

    CTCoreMessage *message = [[self allMessages] objectAtIndex:row];

    // Sender

    CTCoreAddress *sender = [message sender];
    NSString *senderName = [sender name];

    // Subject

    NSString *subject = [message subject];
    if ([subject length] == 0)
    {
        subject = @"(No Subject)";
    }

    // Body

    BOOL isPlain = YES;
    NSString *body = [message bodyPreferringPlainText:&isPlain];
    body = [[body componentsSeparatedByCharactersInSet:
             [NSCharacterSet whitespaceAndNewlineCharacterSet]]
            componentsJoinedByString:@" "];
    body = [body stringByReplacingOccurrencesOfString:@"  " withString:@" "];

    // Populate Cell

    [[cell nameText] setText:senderName];
    [[cell subjectField] setText:subject];
    [[cell messageText] setText:body];

    if ([message isUnread])
    {
        cell.nameText.textColor = [UIColor colorWithRed:15.0/255.0 green:140.0/255.0 blue:198.0/255.0 alpha:1];
    }
    else
    {
        cell.nameText.textColor = [UIColor blackColor];
    }

    return cell;

}

【问题讨论】:

    标签: iphone performance uitableview sdk scroll


    【解决方案1】:

    xCode 带有一个称为 Instruments 的分析器。它的 CPU 时间分析器非常适合找出哪些代码正在减慢速度。使用分析器运行您的应用程序并花几秒钟滚动浏览。它会给你统计数据。

    请记住,if (cell == nil) 中的代码将运行大约 10 次(UITableView 缓存的单元格刚好可以填充自己)。但是 if 之外的代码很昂贵 - 每次单元格可见时它都会运行。

    我猜你发布的代码中最昂贵的操作是:

    给 iOS 太多子视图以在单元格上绘制

    • 改为自己绘制。

    将整个正文文本中的空格替换为单个空格

    • 您发布的代码为每个单词分配了新的字符串,加上一个数组来保存它们。然后它再分配两个副本(一个带有重新连接的单词,另一个带有压缩的空格)。它会处理整个正文文本字符串,即使用户在正文的微小预览中永远不会看到大部分内容!
    • 缓存结果字符串,以便每个单元仅执行一次此操作。
    • 此外,您可以创建一个新的可变字符串,在其中保留空间,并在循环中从原始字符串复制字符(空格除外)。而不是处理整个正文文本,您可以在 100 个字符左右停止(足以填充表格单元格)。速度更快,节省内存。

    UITableView 滚动缓慢是一个非常常见的问题。见:
    How to solve slow scrolling in UITableView
    iPhone UITableView stutters with custom cells. How can I get it to scroll smoothly?

    【讨论】:

      【解决方案2】:

      您的代码似乎没有问题。我建议使用表优化框架,例如免费的 Sensible TableView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        相关资源
        最近更新 更多