【问题标题】:How to improve UITableView scrolling performance? What I am I missing?如何提高 UITableView 的滚动性能?我错过了什么?
【发布时间】:2014-07-11 00:13:27
【问题描述】:

我知道基础知识,在cellForRowAtIndexPath: 中不能做什么,这可能会导致滚动性能受到阻碍。我相信我已经遵守了这些规则,这就是我能走到这一步的原因。我的UITableView 在滚动时很糟糕,做得很好,但有时它会在几秒钟到几秒钟内断断续续,当它开始放慢一点时会很明显。

如果不透露太多我的代码,我在做什么可能会导致这种情况。我想我已经检查了一切并消除了可能导致它的东西,但问题仍然存在。我觉得好像这是很明显的事情,我正在俯瞰。非常感谢您的帮助。

谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"tweetCell";

    TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSDictionary *tweet = _tweets[indexPath.row];

    NSString *username = tweet[@"username"];

    CGFloat tweetHeight = [tweet[@"contentHeight"] floatValue];

    cell.tweet.frame = ({
        CGRect frame = cell.tweet.frame;
        frame.size.height = tweetHeight + 2;
        frame.origin.y = cell.bounds.size.height / 2 - frame.size.height / 2;
        frame;
    });

    cell.tweet.attributedText = tweet[@"attributedText"];

    cell.imageView.image = [_profilePhotos[username] valueForKey:@"image"];

    cell.date.text = tweet[@"dateString"];

    if (tweet[@"media"]) {

        cell.tweetImage.image = tweet[@"media"];
        cell.tweetImage.hidden = NO;

    } else {

        cell.tweetImage.image = nil;
        cell.tweetImage.hidden = YES;
    }

    NSMutableAttributedString *attributedText = [tweet[@"attributedText"] mutableCopy];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"useDynamicTextSize"]) {

        UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

        if (cell.tweet.font.pointSize != font.pointSize) {
            cell.tweet.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:font.pointSize];
            cell.username.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:font.pointSize];
            cell.date.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:font.pointSize];

            [attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:font.pointSize] range:NSMakeRange(0, attributedText.length)];

        } else {
            [attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:cell.tweet.font.pointSize] range:NSMakeRange(0, attributedText.length)];
        }
    } else {
        [attributedText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:17] range:NSMakeRange(0, attributedText.length)];
    }

    cell.tweet.attributedText = attributedText;

    cell.username.adjustsFontSizeToFitWidth = YES;

    NSString *color = [[NSUserDefaults standardUserDefaults] objectForKey:@"color"];
    if ([color isEqualToString:@"automatic"]) {
        color = ([[UIScreen mainScreen] brightness] <= .5) ? @"black" : @"white";
    }
    [cell.imageView.layer setMasksToBounds:YES];
    [cell.imageView.layer setCornerRadius:5];
    [cell.imageView.layer setBorderColor:[[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.55] CGColor]];
    [cell.imageView.layer setBorderWidth:0.5];

    [cell.tweetImage.layer setMasksToBounds:YES];
    [cell.tweetImage.layer setCornerRadius:5];
    [cell.tweetImage.layer setBorderColor:[[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.55] CGColor]];
    [cell.tweetImage.layer setBorderWidth:0.65];

    //  [UIView beginAnimations:nil context:nil];
    //  [UIView setAnimationDuration:0.25];
    //  [UIView setAnimationDelegate:self];

    if ([color isEqualToString: @"white"]) {
        cell.tweet.textColor =[UIColor blackColor];
        cell.date.textColor = [UIColor blackColor];
        cell.username.textColor = [UIColor blackColor];
        cell.backgroundColor = [UIColor whiteColor];
        cell.tweet.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};

        [tweet[@"attributedText"] addAttribute:NSForegroundColorAttributeName
                                         value:[UIColor blackColor]
                                         range:NSMakeRange(0, cell.tweet.attributedText.length)];

    } else /*if ([color isEqualToString: @"black"])*/ {
        cell.tweet.textColor = [UIColor whiteColor];
        cell.date.textColor = [UIColor whiteColor];
        cell.username.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
        cell.tweet.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor colorWithRed:0.66 green:0.82 blue:1 alpha:1]};

        [tweet[@"attributedText"] addAttribute:NSForegroundColorAttributeName
                                         value:[UIColor whiteColor]
                                         range:NSMakeRange(0, cell.tweet.attributedText.length)];
    }

    //  [UIView commitAnimations];

    if (_retweets[_tweets[indexPath.row][@"id"]]) {
        if ([color isEqualToString: @"white"]) {
            cell.backgroundColor = [UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1];

        } else if ([color isEqualToString: @"black"]) {
            cell.backgroundColor = [UIColor colorWithRed:0.114 green:0.114 blue:0.114 alpha:1];
        }

        //        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);

    } else {
        if ([color isEqualToString: @"white"]) {
            [cell setBackgroundColor:[UIColor whiteColor]];
        } else if ([color isEqualToString: @"black"]) {
            cell.backgroundColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
        }

        //        cell.separatorInset = UIEdgeInsetsMake(0, 80, 0, 0);
    }

    cell.tweet.tag = indexPath.row;
    cell.tweetImage.userInteractionEnabled = YES;

    cell.tweet.frame = ({
        CGRect frame = cell.tweet.frame;
        frame.size.height = tweetHeight + 2;
        frame.origin.y = cell.bounds.size.height / 2 - frame.size.height / 2;
        if (tweet[@"media"]) {
            frame.size.width = 164;
        } else {
            frame.size.width = 224;
        }
        frame;
    });

    cell.tweet.delegate = self;

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(retweetTweet:)];
    doubleTap.numberOfTapsRequired = 2;
    [cell addGestureRecognizer:doubleTap];
    [cell.tweet addGestureRecognizer:doubleTap];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
    tap.numberOfTapsRequired = 1;
    [cell.tweetImage addGestureRecognizer:tap];

    UITapGestureRecognizer *tapToViewProfile = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewProfile:)];
    tap.numberOfTapsRequired = 1;
    [cell.imageView setUserInteractionEnabled:YES];
    [cell.imageView addGestureRecognizer:tapToViewProfile];

    return cell;
}

TweetCell.m:

//
//  TweetCell.m
//  Khabara
//
//  Created by Isa Ranjha on 3/26/14.
//  Copyright (c) 2014 Isa Ranjha. All rights reserved.
//

#import "TweetCell.h"

@implementation TweetCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,45,45);
    self.imageView.frame = ({
        CGRect frame = self.imageView.frame;
        frame.size = CGSizeMake(48, 48);
        frame.origin.y = self.bounds.size.height / 2 - frame.size.height / 2;
        frame;
    });

    self.imageView.backgroundColor = [UIColor whiteColor];

}

- (void)awakeFromNib
{
    //_tweet.numberOfLines = 0;
    _tweet.textContainerInset = UIEdgeInsetsZero;
    _tweet.canCancelContentTouches = YES;


    //    _tweet.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

【问题讨论】:

  • 这就是 Instruments 的用途。分析应用程序。避免猜测。
  • 顺便说一句 - 你有一个严重的错误。当您滚动时,您会不断向单元格添加越来越多的手势识别器,并且这些单元格会被重复使用。
  • 另一个建议 - 您有一个自定义单元格 (TweetCell)。如果你有一个自定义单元类,为什么这么多单元逻辑和设置仍在视图控制器中?把它全部放在单元格类中。
  • 哇,完全错过了 UIGestureRecognizer,谢谢。我会考虑在单元格的课堂上这样做。
  • 您还可以移动所有常用代码,例如设置图层属性、单元格内的框架,因为它们保持不变,您无需每次都重做。您还可以使用两种类型的单元格,它们具有不同的字体并具有不同的重用标识符。根据类型,您可以使用一种或另一种细胞类型。

标签: ios objective-c uitableview smooth-scrolling


【解决方案1】:

不要在单元格上使用设置的图层角半径。它使问题。而不是这个,你最好创建一个框架 .png 图像。

【讨论】:

  • 我注释掉了,滚动性能没有太大区别?
  • attributeText 怎么样。我很确定这是画东西的问题。注释掉所有内容并取消注释每一行以跟踪。
  • 注释掉了,一样。单元格现在没有文本了。
  • Time Profiler 说这条线花费的时间最多。 TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
猜你喜欢
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多