I am having an issue correctly setting the image for UIButtons in a UITableViewCell. I am using a custom Cell in Storyboard with Four buttons to create a Gallery effect and loading the images from a server using an ImageLoader Delegate like in Apple’s LazyTableImages’ sample code. I am getting the images back from the ImageLoader just fine, the problem is setting the image.
If I set the images in the UITableView cellForRowAtIndexPath, I can set the images fine if they are already loaded or just default images:
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];
}
However when the delegate calls the ImageDidLoad method later on, the images are not changing.
- (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];
}
}
}
You can see I have a UIImage in the cell for testing. This is working just fine. This code also works fine if I remove the for loop in the cellForRowAtIndexPath up above. The problem is without that for loop, the button images are not set if the images are already loaded.
I’m not sure if this is a problem with too many pointers preventing the UIButton from changing. That is all I can think of, but I am pretty new with StoryBoard and this style of Custom Cell manipulation. Thanks for any help.
Up top you set the button tag as
[button setTag:post.userID];but down the bottom you retrieve the button using the tag:int buttonTag = 200 + postColumn + 1;