我正在尝试实现像 facebook 或 twitter 时间线这样的表格。但是当我滚动 时UITableView
,它非常缓慢且滞后。我该如何解决这个问题?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *dataArray = [self.threndsArray objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];
NSData *imgData = [NSData dataWithContentsOfURL:url];
cell.upload_image.image =[UIImage imageWithData:imgData];
NSURL *urlimage = [NSURL URLWithString:[dataArray objectForKey:@"pic_user"]];
NSData *Dataimage = [NSData dataWithContentsOfURL:urlimage];
cell.uploder_image.image =[UIImage imageWithData:Dataimage];
cell.uploder_image.clipsToBounds = YES;
cell.uploder_image.layer.cornerRadius = cell.uploder_image.frame.size.height /2;
cell.uploder_image.layer.borderWidth = 2.0;
cell.uploder_image.layer.borderColor = [UIColor whiteColor].CGColor;
cell.uploder_name.text=[dataArray valueForKeyPath:@"nama_user"];
cell.like_counter.text=[NSString stringWithFormat:@"%@",[dataArray valueForKeyPath:@"likes"]];
cell.commenter_name.text=[dataArray valueForKeyPath:@"status_timeline"];
cell.comments.text=[dataArray valueForKeyPath:@"tgl_timeline"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
最好的方法是SDWebImageView Download and Use Like Bellow :
在 .h 中导入 2 个文件
#import "SDWebImage/UIImageView+WebCache.h" #import "UIImageView+UIActivityIndicatorForSDWebImage.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *dataArray = [self.threndsArray objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[dataArray objectForKey:@"pic_timeline"]];
[cell.upload_image setImageWithURL:[dataArray objectForKey:@"pic_timeline"] placeholderImage:nil usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.uploder_image.clipsToBounds = YES;
cell.uploder_image.layer.cornerRadius = cell.uploder_image.frame.size.height /2;
cell.uploder_image.layer.borderWidth = 2.0;
cell.uploder_image.layer.borderColor = [UIColor whiteColor].CGColor;
cell.uploder_name.text=[dataArray valueForKeyPath:@"nama_user"];
cell.like_counter.text=[NSString stringWithFormat:@"%@",[dataArray valueForKeyPath:@"likes"]];
cell.commenter_name.text=[dataArray valueForKeyPath:@"status_timeline"];
cell.comments.text=[dataArray valueForKeyPath:@"tgl_timeline"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句