我需要先从远程获取图像列表,然后UIImageView
才能添加到UITableViewCell
,所以我首先渲染没有图像的表格,然后在列表的异步获取回调中将图像添加到单元格,但我发现在表格之后 addSubview 不起作用已渲染。
我试图添加[tableView reloadData]
,但它没有解决它。
ViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
// [cell setBackgroundImg:@"http://www.domain.com/uploads/2013/0708/big_886a91103d2.jpg" maskImg:nil fromTable: tableView];
// above line works
return cell;
}
/* this is the callback called after the list of image loaded.
* I'm sure it works like the line I commented out above, but it can't add subview
*/
- (void)addCellImages:(NSDictionary *)images
{
for (int i = 0; i < [images count]; i++)
{
int row = (i + 1) * 2;
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
MyCell *cell = (MyCell *)[self tableView:table cellForRowAtIndexPath:path];
if (row == 2)
[cell setBackgroundImg:[images valueForKey:@"shop"] maskImg:nil fromTable:table];
if (row == 4)
[cell setBackgroundImg:[images valueForKey:@"guide"] maskImg:nil fromTable:table];
if (row == 6)
[cell setBackgroundImg:[images valueForKey:@"coupon"] maskImg:nil fromTable:table];
if (row == 8)
[cell setBackgroundImg:[images valueForKey:@"prize"] maskImg:nil fromTable:table];
}
}
我的细胞
- (void)setBackgroundImg:(NSString *)backgroundImg maskImg:(NSString *)maskImg fromTable: (UITableView *)table
{
NSURL *bgURL = [[NSURL alloc] initWithString:backgroundImg];
UIImageView *bgImageView = [[UIImageView alloc] init];
[bgImageView setImageWithURL:bgURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){[table reloadData];}];
// Above line uses SDWebImage to load image async, I thought reloadData can make it work, but it didn't
bgImageView.frame = CGRectMake(0, 0, 240., 70.);
[self addSubview:bgImageView];
}