1

我需要先从远程获取图像列表,然后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];
}
4

2 回答 2

0

cellForRowAtIndexPath您始终创建并返回一个新的单元格实例时,这意味着您因异步下载而进行的任何修改都会在您重新加载表格时自动丢弃(因为您刚刚添加子视图的单元格已被破坏)。如果您不重新加载表格,它应该可以工作。


如果你打电话addCellImages那也将是一个问题。您需要在调用之前切换回主线程,因为 UI 只能从主线程更新。


你在哪里:

    MyCell *cell = (MyCell *)[self tableView:table cellForRowAtIndexPath:path];

这威尔也创造了一个新的细胞。您要做的是从表格视图中获取当前单元格:

    MyCell *cell = (MyCell *)[table cellForRowAtIndexPath:path];
于 2013-07-20T22:20:01.803 回答
0

您应该尝试Full-Loaded - 当涉及到这些问题时非常方便。

于 2013-07-21T07:05:56.367 回答