Ios 如何提高UITableView滚动性能?我错过了什么?
我知道在cellForRowAtIndexPath:中不应执行的基本操作,这可能会影响滚动性能。我相信我遵守了这些规则,这就是为什么我走到了这一步。我的UITableView在滚动方面非常糟糕,做得非常好,但有时它会在几秒钟到几秒钟之间断断续续,当它开始稍微慢一点时,就很明显了 在不透露太多代码的情况下,我在做什么可能导致这种情况。我想我已经检查了所有的东西,排除了可能导致它的原因,但问题仍然存在。我觉得这是一件显而易见的事情,我正在忽略它。非常感谢您的帮助 多谢各位Ios 如何提高UITableView滚动性能?我错过了什么?,ios,objective-c,uitableview,smooth-scrolling,Ios,Objective C,Uitableview,Smooth Scrolling,我知道在cellForRowAtIndexPath:中不应执行的基本操作,这可能会影响滚动性能。我相信我遵守了这些规则,这就是为什么我走到了这一步。我的UITableView在滚动方面非常糟糕,做得非常好,但有时它会在几秒钟到几秒钟之间断断续续,当它开始稍微慢一点时,就很明显了 在不透露太多代码的情况下,我在做什么可能导致这种情况。我想我已经检查了所有的东西,排除了可能导致它的原因,但问题仍然存在。我觉得这是一件显而易见的事情,我正在忽略它。非常感谢您的帮助 多谢各位 - (UITableVie
- (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;
}
不要在单元上使用设置的图层角半径。这就是问题所在。与此相反,您最好创建一个frame.png图像。这就是Instruments的用途。配置应用程序。避免猜测。顺便说一句,你有一个严重的错误。当你滚动时,你会不断地向单元格中添加越来越多的手势识别器,这些单元格会被重新使用。还有一个建议——你有一个自定义的单元格TweetCell。如果您有一个自定义单元类,为什么视图控制器中还有这么多单元逻辑和设置?把它全部放在细胞类中。哇,完全错过了UIgestureRecognitor一,谢谢。我将在cell的类中研究这一点。此外,您可以移动所有常用代码,例如设置图层属性,在单元格内设置边框,因为它们保持不变,您无需每次都重复。您还可以使用两种类型的单元格,它们具有不同的字体和外观以及不同的重用标识符。根据类型,您可以使用一种单元格类型或另一种单元格类型。我对其进行了注释,在滚动性能方面没有太大差异?AttributeText呢。我很确定这是画画的问题。注释掉所有内容并取消注释每一行以跟踪。注释掉,相同。单元格现在没有文本。时间档案器说这行占用的时间最多。TweetCell*cell=TweetCell*[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//
// 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