我的问题是----我在UITableView
中使用custom cell
时在tableview
中加载数据时它正在加载描述并且标题也是日期但不在tableview中加载图像。将所有图像加载到一行中。当我使用此代码时,滚动效果很好,但不能在TableView
中加载图像。我想要延迟加载概念。转到下面的链接。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
newsobject=(newscustamCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (newsobject ==nil)
{
[[NSBundle mainBundle]loadNibNamed:@"newscustamCell" owner:self options:nil];
}
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]];
/* Fetch the image from the server... */
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
/* This is the main thread again, where we set the tableView's image to
be what we just fetched. */
newsobject.allnewsimg.image = img;
// newsobject.allnewsimg.image=[UIImage imageWithData:data];
});
});
newsobject.allnewsdes.text=[[[newsarry objectAtIndex:indexPath.row] objectForKey:@"long_desc"]stringByStrippingTags];
newsobject.allnewslabel.text=[[newsarry objectAtIndex:indexPath.row] objectForKey:@"title"];
![enter image description here][1]
newsobject.timelabel.text=[[newsarry objectAtIndex:indexPath.row] objectForKey:@"date"];
return newsobject;
}
答案 0 :(得分:4)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[tableView cellForRowAtIndexPath:indexPath].imageView.image = image;
});
});
答案 1 :(得分:1)
您可以使用某些Lib
,例如SDWebImage
或Afnetworking
。 Libs
支持延迟加载图像并自动缓存此图像。
所有你需要做的:
[cell.imageView setImageWithURL:[NSURL URLWithString:@"url of image"]
placeholderImage:nil];
答案 2 :(得分:0)
我会与你分享一些代码。请遵循此代码。
首先,您必须从服务器检索所有数据。在tableview
上加载之后。
试试这段代码:
- (void)viewDidLoad
{
NSString *urlString=@"http://put your url";
self.responseData=[NSMutableData data];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self];
NSLog(@"connection====%@",connection);
}
JSON委托方法-------------
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
self.responseData=nil;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSArray *response_Array = [NSJSONSerialization JSONObjectWithData:self.responseRankData options:kNilOptions error:nil];
NSDictionary *dataDictionary=nil;
NSDictionary *imgurlDic=nil;
NSArray *imgDic=nil;
// I am retrieve image according to my url. So please edit this according to your url. i am just write the format.
for (int i=0; i < response_Array.count; i++)
{
imgurlDic = [imgDic objectAtIndex:i];
imgDic = [dataDictionary objectForKey:@"imageKey"]; // Take your Key according to URL
NSURL *url = [NSURL URLWithString:[imgurlDic objectForKey:@"url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self.Img_Array addObject:img]; // Adding all image in your image Array.
NSLog(@"imgurlDic == %@",imgurlDic);
}
[self.tableview reloadData]; // reload your tableview here.
}
获取阵列中的所有图像,您可以在cellForRowAtIndexPath
方法或要加载图像的位置使用此数组。
答案 3 :(得分:0)
由于表格视图单元格快速排队/重复使用的方式,当您的网络请求完成加载时所指的单元格可能与您认为不一样(因为它已脱离屏幕并排队等待)然后重新分配给另一个indexPath
)。此外,在返回单元格之前,您需要确保将其imageView
的{{1}}属性重置为image
或某个默认值。有关详情,请参阅我的回答:https://stackoverflow.com/a/23473911/91458
此外 - 保留已检索图像的缓存是个好主意。检查nil
类,这与使用可变字典但具有内置内存管理等非常相似。
答案 4 :(得分:0)
我也使用了这个技巧,但是线程和线程之间的索引弄得一团糟,所以我搜索了一下,我在GitHub上找到了一个很棒的库。
您只需要#import <SDWebImage/UIImageView+WebCache.h>
到项目中,并且只需使用以下代码即可在下载图像时定义占位符:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
CustomCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
MyObject *object = (MyObject *)[self.list.array objectAtIndex:indexPath.row];
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
return cell;
}
它还可以缓存下载的图像并为您提供出色的性能。
希望它会对你有所帮助!
答案 5 :(得分:0)
我希望此链接对您有所帮助。通过使用asyn类图像加载异步...