zoukankan      html  css  js  c++  java
  • iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    (2015-12-05 12:48:20)[编辑][删除]
       

    首先我们先明确一下问题:

    1.因为UI是在主线程中更新的,不能在down数据的同时显示界面,这样会使得下载的时间占用主线程,导致的后果就是你的屏幕就那样的卡死在哪了
    2.如果要解觉问题1,就势必要将其下载数据的环节放在其他分线程上来实现,但是这里还会遇见一个问题,分线程的执行是不会有序的,这样,在动态显示的过 程中,cell中的数据就会混乱的变化(这里解释的不够清晰,主要是因为,在传递数据时,本人用的是属性数组,应为多线程存入数组的次序是乱序的,这里我 已经用NSoperation队列实现了有序)
     
    解决方案: 将数据的下载提前到ViewDidLoad中的多线程中进行,在tableview的问题3中实现方法是:判断数组中的数据是否完全下载结束,如果没有下载完,就用系统预设的方案来显示,判断下载完了再在动态滑动的时候去更新这些数据,
    注意:这里有一个很大的问题,目前还为解决,因为数据是连续的,不可能都存放到数组中,这样会导致站的内存变大,所以还希望有大神解决一下
     
    附上demo

    //

    //  PIcTableViewController.h

    //  故事版自定义Cell 内加线程

    //

    //  Created by tareba on 15/12/2.

    //  Copyright © 2015年 tanada. All rights reserved.

    //

    #import

    @interface PIcTableViewController : UITableViewController

    @end

    //

    //  PIcTableViewController.m

    //  故事版自定义Cell 内加线程

    //

    //  Created by tareba on 15/12/2.

    //  Copyright © 2015年 tanada. All rights reserved.

    //

    //#import "AppRecord.h"

    #import "PIcTableViewController.h"

    #import "NewCell.h"

    @interface PIcTableViewController ()

    @property (nonatomic,strong) NSMutableArray * arrIamge;

    @property(nonatomic,strong) NSOperationQueue *operationQueue;

    @property (nonatomic,strong)NSArray*imgURLs;

    @property (nonatomic,strong)UIImage *image;

    @end

    @implementation PIcTableViewController

    - (NSArray *)imgURLs {

        if(_imgURLs == nil) {

            NSString*i1=@"http://down.tutu001.com/d/file/20101129/2f5ca0f1c9b6d02ea87df74fcc_560.jpg";

            NSString *i2=@"http://img.taopic.com/uploads/allimg/130501/240451-13050106450911.jpg";

            NSString* i3=@"http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg";

            NSString* i4=@"http://pic.nipic.com/2007-11-09/200711912230489_2.jpg";

            _imgURLs =@[[NSURL URLWithString:i1],[NSURL URLWithString:i2],[NSURL URLWithString:i3],[NSURL URLWithString:i4] ];

        }

        return _imgURLs;

    }

    -(void)downloadImage:(NSArray*)arrURLs{

        for (int i=0; icount; i++) {

         

            NSData *data=[NSData dataWithContentsOfURL:self.imgURLs[i]];

            UIImage *image=[UIImage imageWithData:data];

            [self.arrIamge addObject: image];

        }

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        NSInvocationOperation*op=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downloadImage:) object:self.imgURLs];

        NSOperationQueue *queue=[[NSOperationQueue alloc]init];

        [queue addOperation:op];

        self.tableView.backgroundColor=[UIColor whiteColor];

        [self.tableView registerClass:[NewCell class] forCellReuseIdentifier:@"Cell"];

        // Uncomment the following line to preserve selection between presentations.

        // self.clearsSelectionOnViewWillAppear = NO;

        

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

        // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    }

     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

         return 200;

     }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    //#warning Incomplete implementation, return the number of sections

        return 1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //#warning Incomplete implementation, return the number of rows

        return 12;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        NewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

        

        

       

       

        if (self.arrIamge.count!=self.imgURLs.count) {

            cell.imageview.image=[UIImage imageNamed:@"Lion.jpg"];

            NSLog(@"%@",cell.imageview.image);

            return cell;

        }else{

            NSInteger index=indexPath.row%self.arrIamge.count;

            

                 cell.imageview.image=self.arrIamge[index];

           

           

            return cell;

        }

        

        

    //    dispatch_async( dispatch_get_main_queue(), ^{

    //        cell.imageview.image=self.image;

    //         NSLog(@"2 %@",[NSThread currentThread]);

    //     });

    //

        

       

        

    }

    - (NSMutableArray *) arrIamge {

    if(_arrIamge == nil) {

            _arrIamge = [NSMutableArray array ];

    }

    return _arrIamge;

    }

    @end

    自定义的cell

    //

    //  NewCell.h

    //  故事版自定义Cell 内加线程

    //

    //  Created by tareba on 15/12/2.

    //  Copyright © 2015年 tanada. All rights reserved.

    //

    #import

    @interface NewCell : UITableViewCell

    @property (strong, nonatomic) UIImageView *imageview;

    @end

    //

    //  NewCell.m

    //  故事版自定义Cell 内加线程

    //

    //  Created by tareba on 15/12/2.

    //  Copyright © 2015年 tanada. All rights reserved.

    //

    #import "NewCell.h"

    @implementation NewCell

    - (UIImageView *)imageview {

        if(_imageview == nil) {

            _imageview = [[UIImageView alloc] init];

            _imageview.backgroundColor=[UIColor redColor];

            [self.contentView addSubview:_imageview];

            [_imageview mas_makeConstraints:^(MASConstraintMaker *make) {

                make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));

            }];

            self.contentView.contentMode=UIViewContentModeScaleAspectFit;

            

        }

        return _imageview;

    }

    - (void)awakeFromNib {

        // Initialization code

    }

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {

        [super setSelected:selected animated:animated];

        // Configure the view for the selected state

    }

    @end

    追寻最真
  • 相关阅读:
    Atitit.安全性方案规划设计4gm  v1 q928
    Atitit ati licenseService    设计原理
    Atitit.js图表控件总结
    Atitit. null错误的设计 使用Optional来处理null
    System.Web.Mvc 命名空间
    provider: SQL Network Interfaces, error: 26 Error Locating Server/Instance Specified
    Visual Studio 2010 实用功能总结
    My First J2ME
    Java开发利器ideaIU最新版本10.5的keygen
    Happy New Year for 2012
  • 原文地址:https://www.cnblogs.com/zhao-jie-li/p/5128481.html
Copyright © 2011-2022 走看看