原文地址:如何提高tableView的滚动速度
作者:theonelgq
大家应该知道吧, tableview的每个cell上如果加一些文字图片而且cell很多的情况下 tableview会变的相当的卡,这样用户体验非常的差。如何才能提高tableview的滚动速度呢?我现在就凭借我的经验来给大家说上几点:
首先我先简单的说一下tableview的cell的机制;每个cell都会重用的,也就是说当我们滑动tableview的时候cell上的内容是不停的被释放和生成的,于是cell上面的内容的多少就会影响到整个tableview的滑动速度的。
接下来就所做的这些内容就是为了克服这一点的。
(1)尽量减少cell上面的子view。
这点应该很好理解,cell上面的内容少了tableview的速度自然快了。
(2)使用多线程。
大家在做cell的时候应该都是在cell上面放些图片什么的,加载这些内容是要阻塞线程的,如果这些能过在另一个线程下进行的话,主线程是不是负载就小了呢?于是会大大提高tableview的滚动速度。
(3)画出cell (是我重点想说的)
如果cell只加载一个view,大家觉得cell的负担会不会很少呢?答案是肯定的。平常都是在cell上加入好几个Label等一些内容,如果这些东西能够用一个视图来呈现的话那就会省去很多资源,这就用到了画cell。
先分享一下代码:
#import <Foundation/Foundation.h>
@interface TableViewCellView : UIView
{
NSString *m_Title,*m_time,*m_videoLength;
UIImage *m_videoImage;
BOOL m_BeSecleted,m_select;
}
@property(nonatomic,retain)NSString *m_Title,*m_time,*m_videoLength;
@property(nonatomic,retain)UIImage *m_videoImage;
@property(nonatomic, readwrite)BOOL m_BeSecleted;
@end
"TableViewCellView.h"
@implementation TableViewCellView
@synthesize m_Title,m_time,m_videoLength,m_videoImage,m_BeSecleted;
-(void)drawCell
{
CGContextRef context1 = UIGraphicsGetCurrentContext();
CGContextSaveGState(context1);
if (m_BeSecleted)
{
if (m_select)
{
CGContextSetRGBFillColor(context1,1,1,1,1);
}
else
{
CGContextSetRGBFillColor(context1, 0.348, 0.105, 0.489, 1);
}
}
else
{
if (m_select)
{
CGContextSetRGBFillColor(context1, 1, 1, 1, 1);
}
else
{
CGContextSetRGBFillColor(context1, 0.132, 0.348, 0.605, 1);
}
}
[m_videoImage drawInRect:CGRectMake(10, 10, 80, 60)];
[m_Title drawInRect:CGRectMake(110, 4, 164 ,40) withFont:[UIFont systemFontOfSize:17.0]lineBreakMode:UILineBreakModeWordWrap];
CGContextRestoreGState(context1);
CGContextRef context2 = UIGraphicsGetCurrentContext();
CGContextSaveGState(context2);
if (m_select)
{
CGContextSetRGBFillColor(context2, 1, 1, 1, 1);
}
else
{
CGContextSetRGBFillColor(context2, 0.6, 0.6,0.6, 1);
}
[m_time drawInRect:CGRectMake(110, 50, 150 ,20) withFont:[UIFont systemFontOfSize:15.0]lineBreakMode:UILineBreakModeTailTruncation];
[m_videoLength drawInRect:CGRectMake(220,50,120,20) withFont:[UIFont systemFontOfSize:15.0]lineBreakMode:UILineBreakModeTailTruncation];
CGContextRestoreGState(context2);
}
-(void)drawRect:(CGRect)rect
{
[self drawCell];
}
-(void)dealloc
{
[m_videoLength release];
[m_videoImage release];
[m_time release];
[m_Title release];
[super dealloc];
}
@end
大家看到代码因该对整个原理有所了解了吧,下面我就对大家一一讲演一下。
首先这个创建的类是基于:UIview的,View上面画的是内容了,而不是视图了,就像NSString 和iamge之类的。之前他们需要容器来放(nsstring 用UILabel,iamge用UIimageView),现在就把这些东西整合到一个容器中来,就是我们所建的类里。
东西不多!大家自己试试就好了!