转载自: http://blog.csdn.net/ahut_qyb_6737/article/details/40891683
UITableView是一个非常常用的基本视图,在各类app中随处可见。对于一般布局简单的tableView,性能上基本上看不出来什么问题。但是对于cell中视图繁多的tableView,有时候可能就会出现滑动不流畅的现象,以下是本人的一些解决方案,仅供参考。
1."让出"主线程,让主线程减负。所谓"让出"主线程,指的是不要什么操作都放在主线程里。放在主线程中的一般都是视图相关的操作,比如添加子视图、更新子视图、删除子视图等。
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- //处理一些跟当前视图没关系的事情
- //...
- //只用来操作与当前视图有关系的事情,比如:刷新tableView
- dispatch_async(dispatch_get_main_queue(), ^{
- [tableView reload];
- });
- });
2.正确重用cell。正确重用cell不仅仅要重用cell视图,还需要好好重用cell的子视图。
- static NSString *Identifier = @"WeatherCell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:<#(UITableViewCellStyle)#>
- reuseIdentifier:<#(NSString *)#>]
- }
上面的代码在有cell可重用的时候,不会再创建新的cell,但是下面的一句话基本上会让重用粉身碎骨。
- //清空子视图
- [cell.contentView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
- [obj removeFromSuperview];
- }];
举例来说:
对于上面这样的cell,虽然赶不上微博的复杂程度,但是子视图数量也是挺多的,不合理的处理,会使得页面滑动时有明显卡顿感。实现方案如下:
1.图片实现异步下载、非主线程。
- //门店商品图片描述
- UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:Merchant_Table_Cell_Desc_Image_View_Tag];
- imageView.alpha = 1;
- [imageView setImageWithDict:[NSDictionary dictionaryWithObject:merchant.imageName?merchant.imageName:@"" forKey:@"file_name"]];
- //下载时让出主线程
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
- //指示器
- UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- indicator.frame = CGRectMake((self.frame.size.width - 30)/2 , (self.frame.size.height - 30)/2, 30, 30);
- [indicator startAnimating];
- [self addSubview:indicator];
- //待加载图片的名称(我的车库图片为:cars/A3.jpg格式)
- NSString *fileName = [dict valueForKey:@"file_name"];
- //缓存文件全路径
- NSString *filePath = [[datas objectForKey:@"documentPath"] stringByAppendingPathComponent:[fileName lastPathComponent]];
- NSFileManager *manager = [NSFileManager defaultManager];
- //下载文件接口不会创建上级目录,在调用之前创建文件夹路径
- if (![manager fileExistsAtPath:filePath isDirectory:NULL]) {
- [manager createDirectoryAtPath:[datas objectForKey:@"documentPath"] withIntermediateDirectories:YES attributes:nil error:nil];
- }
- NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
- //下载的图片名
- [param setValue:[dict valueForKey:@"file_name"] forKey:@"file_name"];
- //下载图片保存的位置
- [param setValue:filePath forKey:@"filePath"];
- //从网络获取图片
- [VICBaseService downloadFileWithDictionary:param andSuccessBlock:^{
- //操作视图时,移除指示器、更新图片,拿到主线程来做
- dispatch_async(dispatch_get_main_queue(), ^{
- [indicator removeFromSuperview];
- //从本地缓存中获取
- UIImage *image = [UIImage imageWithContentsOfFile:filePath]
- if (!image) {
- [self setDefaultImage:nil withCustomerViewMode:flag andContentMode:mode];
- }else{
- self.image = image;
- [self customerImageViewMode:flag withContentMode:mode];
- }
- });
- } andErrorBlock:^(NSError *error) {
- //如果本地保存了不完整的图片,删除图片(不占用主线程)
- if ([manager fileExistsAtPath:filePath isDirectory:NULL]) {
- [manager removeItemAtPath:filePath error:nil];
- }
- <pre name="code" class="objc"> //操作视图时,移除指示器、更新图片,拿到主线程来做
- dispatch_async(dispatch_get_main_queue(), ^{
- [indicator removeFromSuperview];
- [self setDefaultImageWithImage:image withCustomerViewMode:flag andContentMode:mode];
- });
- } andProgressBlock:^(double progress) {
- }];
- });
2.重用cell并重用cell中的子视图。新建一个UITableViewCell的子类,重写下面的方法。
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- //门店商品图片描述
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 87, 87)];
- //设置子视图的tag,方便重用时后能获取
- imageView.tag = Merchant_Table_Cell_Desc_Image_View_Tag;
- [self.contentView addSubview:imageView];
- //根据这个思路完成所有视图的构建
- }
之前有说到重用cell时会遇到cell子视图重叠的问题或者其它问题,问题的根源都是由于重用时子视图的属性不会被重置。对于重叠问题,思路如下:创建cell时尽可能的使cell包含所有的子视图,并且在-tableView:cellForRowAtIndexPath:创建cell之后调用如下方法:
- [cell.contentView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
- [obj setAlpha:0];
- }];
- UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:Merchant_Table_Cell_Desc_Image_View_Tag];
- imageView.alpha = 1;//只显示我们需要的视图
- [imageView setImageWithDict:[NSDictionary dictionaryWithObject:merchant.imageName?merchant.imageName:@"" forKey:@"file_name"]];