码迷,mamicode.com
首页 > 其他好文 > 详细

优化UITableView滚动性能

时间:2014-05-11 14:55:50      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:ios   uitableview   uitableviewcell   优化滚动性能   

自定义的cell如下:

bubuko.com,布布扣

第一个:Instruments测试,iphone4 38fps-45fps,iphone3G 25fps

// table with normal XIB based cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {

NSArray *nib = [[UINib nibWithNibName:@"CustomCell" bundle:nil] instantiateWithOwner:self options:nil];
cell = (CustomCell*)[nib objectAtIndex:0];
}

// other initialization goes here
    cell.titleLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    cell.subTitleLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    cell.timeTitleLabel.text = @"yesterday";
    cell.thumbnailImage.image = [UIImage imageNamed:@"iOS6"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;
}


第二个,优化之后的代码为,Instruments测试 iphone 4/iphone 4s接近60帧每秒

// table with with built in cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];

    cell.imageView.image = [UIImage imageNamed:@"ios5"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

第三,使用Loren Bricher提供的技术,几乎每台设备都达到了60fps,包括iphone3G

Loren BricherTwitter中写了关于Tweetie中流畅滚动的内容。以下代码就是使用Loren提供的技术来为UITableView创建自定义表单元。

// table with with custom drawn cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomDrawnCell";

CustomDrawnCell *cell = (CustomDrawnCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) 
{
cell = [[CustomDrawnCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

    [cell setTitle:[NSString stringWithFormat:@"Row %d", indexPath.row] 
          subTitle:[NSString stringWithFormat:@"Row %d", indexPath.row] 
              time:@"yesterday"
         thumbnail:[UIImage imageNamed:@"ios5"]];

// other initialization goes here
return cell;
}


ABTableViewCell

#import <UIKit/UIKit.h>

// to use: subclass ABTableViewCell and implement -drawContentView:

@interface ABTableViewCell : UITableViewCell
{
UIView *contentView;
}

- (void)drawContentView:(CGRect)r; // subclasses should implement

@end
#import "ABTableViewCell.h"

@interface ABTableViewCellView : UIView
@end

@implementation ABTableViewCellView

- (void)drawRect:(CGRect)r
{
[(ABTableViewCell *)[self superview] drawContentView:r];
}

@end

@implementation ABTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
contentView = [[ABTableViewCellView alloc]initWithFrame:CGRectZero];
contentView.opaque = YES;
[self addSubview:contentView];
}
return self;
}

- (void)setFrame:(CGRect)f
{
[super setFrame:f];
CGRect b = [self bounds];
b.size.height -= 1; // leave room for the seperator line
[contentView setFrame:b];
}

- (void)setNeedsDisplay
{
[super setNeedsDisplay];
[contentView setNeedsDisplay];
}

- (void)drawContentView:(CGRect)r
{
// subclasses should implement this
}

@end


CustomDrawCell

#import <UIKit/UIKit.h>
#import "ABTableViewCell.h"

@interface CustomDrawnCell : ABTableViewCell  {

    NSString *_title;
    NSString *_subTitle;
    NSString *_timeTitle;
    UIImage *_thumbnail;
}

- (void)setTitle:(NSString*) title subTitle:(NSString*) subTitle time:(NSString*) time thumbnail:(UIImage *)aThumbnail;

@end
#import "CustomDrawnCell.h"

@implementation CustomDrawnCell

static UIFont *titleFont = nil;
static UIFont *subTitleFont = nil;
static UIFont *timeTitleFont = nil;

- (void)setTitle:(NSString*) aTitle subTitle:(NSString*) aSubTitle time:(NSString*) aTimeTitle thumbnail:(UIImage *)aThumbnail
{
    if (_title != aTitle) {
        _title = aTitle;        
    }

    if (_subTitle != aSubTitle) {
        _subTitle = aSubTitle;
    }

    if (_timeTitle != aTimeTitle) {
        _timeTitle = aTimeTitle;
    }

    if (_thumbnail != aThumbnail) {
        _thumbnail = aThumbnail;        
    }

    [self setNeedsDisplay];
}

+(void) initialize
{
    titleFont = [UIFont systemFontOfSize:17];
    subTitleFont = [UIFont systemFontOfSize:13];
    timeTitleFont = [UIFont systemFontOfSize:10];
}

+(void) dealloc
{
    [super dealloc];
}

-(void) drawContentView:(CGRect)r
{    
    static UIColor *titleColor;    
    titleColor = [UIColor darkTextColor];
    static UIColor *subTitleColor;    
    subTitleColor = [UIColor darkGrayColor];
    static UIColor *timeTitleColor;    
    timeTitleColor = [UIColor colorWithRed:0 green:0 blue:255 alpha:0.7];

    CGContextRef context = UIGraphicsGetCurrentContext();

    if(self.highlighted || self.selected)
{
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CGContextSetFillColorWithColor(context, titleColor.CGColor);
}
else
{
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CGContextSetFillColorWithColor(context, titleColor.CGColor);
}

    [titleColor set];
    [_thumbnail drawInRect:CGRectMake(12, 4, 35, 35)];
    [_title drawAtPoint:CGPointMake(54, 3) 
                    forWidth:200 
                    withFont:titleFont 
                    fontSize:17 
               lineBreakMode:UILineBreakModeTailTruncation 
          baselineAdjustment:UIBaselineAdjustmentAlignCenters];    

    [subTitleColor set];
    [_subTitle drawAtPoint:CGPointMake(54, 23) 
               forWidth:200 
               withFont:subTitleFont 
               fontSize:13 
          lineBreakMode:UILineBreakModeTailTruncation 
     baselineAdjustment:UIBaselineAdjustmentAlignCenters];    

    [timeTitleColor set];
    [_timeTitle drawAtPoint:CGPointMake(262, 3) 
                  forWidth:62 
                  withFont:timeTitleFont 
                  fontSize:10 
             lineBreakMode:UILineBreakModeTailTruncation 
        baselineAdjustment:UIBaselineAdjustmentAlignCenters];    

}
@end

具体源码在这里

优化UITableView滚动性能,布布扣,bubuko.com

优化UITableView滚动性能

标签:ios   uitableview   uitableviewcell   优化滚动性能   

原文地址:http://blog.csdn.net/chaoyuan899/article/details/25467617

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!