-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cellIdentifier";
BrandTableViewCell *cell = (BrandTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];//通过xib自定义的一个cell,包括图片+title;
if (cell==nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"BrandTableViewCell" owner:self options:nil] objectAtIndex:0];
}
if (logoUrlStr!=nil) {
NSString *md5Str = [logoUrlStr MD5Hash];
NSString *path = NSHomeDirectory();
path = [path stringByAppendingPathComponent:@"tmp"];
path = [path stringByAppendingPathComponent:md5Str];
NSFileManager *fManager = [[NSFileManager alloc]init];
if ([fManager fileExistsAtPath:path]) {
NSData *data = [NSData dataWithContentsOfFile:path];
cell.brandImgView.image = [UIImage imageWithData:data];
//以上是图片缓存;
}else{
if (brandTableView.dragging == NO && tableView.decelerating == NO) {
MyConnection *myCon = [[MyConnection alloc]initWithUrl:logoUrlStr Delegate:self];
myCon.tag = indexPath.row;
}
//异步下载图片,代理方法中写入缓存,并更新cell.brandImgView.image = [UIImage imageWithData:data];
}
}
return cell;
}
代理方法如下:
-(void)MyConnectionDidFinish:(MyConnection *)con Data:(NSData *)data{
BrandTableViewCell *cell = (BrandTableViewCell *)[brandTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:con.tag inSection:0]];
cell.brandImgView.image = [UIImage imageWithData:data];
}
滑动TableView时明显感觉界面很卡。多滑动几次,会引起手机重启。
个人认为是因为你所有的东西都在cellForRow 里面操作,尤其是本地读取这块,所以会造成卡的现象,而且滑动后会创建多个本地读取,可能对性能上消耗很大,建议你开线程,把你的数据源在线程里组织好了,然后主线程拿出来在cellForRow 里面直接用,这样应该会好,你可以试试。