The solution of multithreading problem about cell data transfer in dynamic TableView in iOS

Source: Internet
Author: User

The solution of multithreading problem about cell data transfer in dynamic TableView in iOS(2015-12-05 12:48:20) [ edit ] [ delete ] reproduced

First, let's clarify the question:

1. Because the UI is updated in the main thread, can not display the interface at the same time down data, so that the download time to occupy the main thread, the result is that your screen is like that of the card dead 2. If you want to solve the problem 1, it is bound to download the data link on other sub-threads to achieve, But here will also meet a problem, sub-threading execution is not orderly, so that in the process of dynamic display, the data in the cell will be chaotic changes (here is not clear enough explanation, mainly because, in the passing of data, I use an array of attributes, should be multi-threaded into the order of the array is disorderly, here I The nsoperation queue has been implemented in order) solution: The download of data to viewdidload in the multi-threaded, in the TableView problem 3 implementation method is: Determine whether the data in the array is completely downloaded, if not finished downloading, Use the System preset scheme to display, judge the download and then in the dynamic sliding time to update the data, note: Here is a big problem, is still to solve, because the data is continuous, it is not possible to store in the array, this will cause the station memory becomes larger, so also want to have the great God to solve Attach Demo

//

PIcTableViewController.h

Story Edition custom Cell Nega thread

//

Created by Tareba on 15/12/2.

COPYRIGHT©2015 year Tanada. All rights reserved.

//

#import

@interface Pictableviewcontroller:uitableviewcontroller

@end

//

Pictableviewcontroller.m

Story Edition custom Cell Nega thread

//

Created by Tareba on 15/12/2.

COPYRIGHT©2015 year 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*[email protected] "http://down.tutu001.com/d/file/20101129/2f5ca0f1c9b6d02ea87df74fcc_560.jpg";

NSString *[email protected] "http://img.taopic.com/uploads/allimg/130501/240451-13050106450911.jpg";

nsstring* [email protected] "http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg";

nsstring* [email protected] "http://pic.nipic.com/2007-11-09/200711912230489_2.jpg";

_imgurls [email protected][[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 a 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

A custom cell

//

NewCell.h

Story Edition custom Cell Nega thread

//

Created by Tareba on 15/12/2.

COPYRIGHT©2015 year Tanada. All rights reserved.

//

#import

@interface Newcell:uitableviewcell

@property (Strong, nonatomic) Uiimageview *imageview;

@end

//

newcell.m

Story Edition custom Cell Nega thread

//

Created by Tareba on 15/12/2.

COPYRIGHT©2015 year 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

The solution of multithreading problem about cell data transfer in dynamic TableView in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.