PDA

View Full Version : Refreshing images in UITableView




veliborsantic74
Sep 12, 2012, 11:22 AM
This problem drives me crazy for 9 days. I am downloading some logos from web and set them in TableView. That works OK with iOS 5 built-in method, but the problem is refreshing images. For refreshing I am using PullRefreshTableViewController by Leah Culver (https://github.com/leah/PullToRefresh).
I tried also with AFNetworking and SDWebImage but the problem still exists.
This is part of code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *osnovnaAdresaString = @"http://www.odds1x2.com/";
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

.
logoKlub1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 40, 75, 16)];
[cell.contentView addSubview:logoKlub1];

logoKlubX = [[UIImageView alloc] initWithFrame:CGRectMake(130, 40, 75, 16)];
[cell.contentView addSubview:logoKlubX];

logoKlub2 = [[UIImageView alloc] initWithFrame:CGRectMake(210, 40, 75, 16)];
[cell.contentView addSubview:logoKlub2];

[self downloadLogoFromAddress:Address1 forClub:logoKlub1]; // Address1 is NSString web address
[self downloadLogoFromAddress:AddressX forClub:logoKlubX]; // AddressX is NSString web address
[self downloadLogoFromAddress:Address2 forClub:logoKlub2]; // Address2 is NSString web address
..
return cell;
}

And method where the logo should sit on its place:
-(void) downloadLogoFromAddress: (NSString *) webAddress forClub: (UIImageView *) clubLogo
{

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:webAddress]];

[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
[[NSOperationQueue mainQueue] addOperationWithBlock:
^{
UIImage* pic = [[UIImage alloc]initWithData:data];
[nizLogoKaoSlike addObject:piv]; // tried set logo from NSMutableArray
NSLog(@"Glavni thread? %d", [NSThread isMainThread]); // yes, it is

clubLogo.image = pic;
clubLogo.image = [nizLogoKaoSlike lastObject];
[clubLogo setNeedsLayout];

}]; }]; }


Instead [clubLogo setNeedsLayout] also tried [clubLogo setNeedsDisplay], but with no luck. After pulling down nizLogoKaoSlike contains refreshed images which number is incremented as I scroll down. I am clueless how to force UIImageViews logoKlub1, logoKlubX and logoKlub2 to refresh. ☹



MattInOz
Sep 12, 2012, 08:49 PM
You might want to have a look at Apple sample code project. Pretty sure there is WWDC video that goes with it.

LazyTableImages (https://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html)

veliborsantic74
Sep 13, 2012, 05:31 AM
EDIT: Interesting, I have noticed that all three images on UIImageViews in the last cell are changed, not only once, but more times - like on the slot machines.