1

我在正确UIButtons设置UITableViewCell. 我在 Storyboard 中使用带有四个按钮的自定义单元来创建画廊效果,并使用 ImageLoader Delegate 从服务器加载图像,就像在 Apple 的 LazyTableImages 示例代码中一样。我从 ImageLoader 取回图像很好,问题是设置图像。

如果我在 中UITableView cellForRowAtIndexPath设置图像,如果它们已经加载或者只是默认图像,我可以将图像设置为罚款:

for (int i = 0; i < [posts count] && i < [galleryButtons count]; i++)
    {
        post = [posts objectAtIndex:i];
        UIButton *button = [galleryButtons objectAtIndex:i];

        [button setTag:post.userID];

        if (!post.userIcon)
        {

            if (tableView.dragging == NO && tableView.decelerating == NO)
            {
                loadFlag = YES;
            }
            [button setImage:[UIImage imageNamed:@"BtDefault.png"] forState:UIControlStateNormal];
        } else
        {
            [button setImage:post.userIcon forState:UIControlStateNormal];
        }
    }

if (loadFlag)
    {
        [self startIconDownload:posts forIndexPath: indexPath];
    }

但是,当委托ImageDidLoad稍后调用该方法时,图像并没有改变。

- (void)postImageDidLoad:(NSIndexPath *)indexPath forColumn:(int)postColumn
{
ImageLoader *imageLoader = [imageDownloadsInProgress objectForKey:indexPath];
Post *post;

if (imageLoader != nil)
{
    UITableViewCell *cell = [timeLineTable cellForRowAtIndexPath:indexPath];


    if (timeLineStyle == 1) //Gallary Style
    {

        int index = (indexPath.row * 4) + postColumn;
        int buttonTag = 200 + postColumn + 1;
        NSLog(@"Button tag is %i", buttonTag);
        post = [currentPosts objectAtIndex:index];
        UIButton *galleryButton = (UIButton *)[cell viewWithTag:buttonTag];
        [galleryButton setImage:post.userIcon forState:UIControlStateNormal];

        UIImageView *imageTester = (UIImageView *)[cell viewWithTag:300];
        [imageTester setImage:post.userIcon];

        [timeLineTable reloadData];

        [cell setHighlighted:YES];

    } else  // Post Style
    {
        UIImageView *userImageView = (UIImageView*)[cell viewWithTag:101];
        UILabel *userNameLabel = (UILabel *)[cell viewWithTag:102];
        [userNameLabel setText:@"Image Loaded"];
        post = [currentPosts objectAtIndex:[indexPath row]];
        [userImageView setImage:post.userIcon];
    }

}
}

你可以看到我UIImage在单元格中有一个用于测试。这工作得很好。如果我删除上面的 for 循环,此代码也可以正常工作cellForRowAtIndexPath。问题在于没有 for 循环,如果图像已经加载,则不会设置按钮图像。

我不确定这是否是指针过多阻止UIButton更改的问题。这就是我能想到的,但我对 StoryBoard 和这种自定义单元格操作风格还很陌生。谢谢你的帮助。

4

1 回答 1

1

在顶部您将按钮标签设置为[button setTag:post.userID];但在底部您使用标签检索按钮:int buttonTag = 200 + postColumn + 1;

于 2013-01-10T10:39:45.590 回答