首先我们先明确一下问题:
//
// 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