在阅读有关同一问题的信息时,我尝试了很多方法,但是找不到合适的答案,因此我决定直接询问。这是我的任务。我希望用户启动我的应用程序,并看到包含从网络加载的图像的单元格,但我只看到标题!
然后我向上滚动,屏幕上看不到某些单元格。
之后,我仅看到该单元格的图像。
这是我的代码:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reusbleCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusbleCell];
if (!cell) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusbleCell];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *imageurl = [[NSURL alloc] initWithString:@"http://cs413627.vk.me/v413627067/604c/gD39fdeTH-E.jpg"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageurl];
dispatch_async(dispatch_get_main_queue(), ^{
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
});
});
[[cell textLabel] setText:@"Text"];
return cell;
}
我创建了一个测试项目以检查原因,我发现如果未设置cell.imageView.image
返回单元格之前的单元格,则cell.imageView.frame将为CGRectZero。因此,当接收到imageData时,您确实将imageView.image设置为imageData ,但图像将永远不会显示。
我认为您可以使用自定义的UIImageView来定制UITableViewCell。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句