Image lazy loading in table view - iphone

I want to use lazy loading of images concept in my table view. My array of image rul string is ( MSMutableArray ) "images". How can I implement this in cellForRowAtIndexPath.
It looks like:
-(UITableViewCell *) tableView:(UITableView *)AtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[searchtable dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.textLabel.font=[UIFont systemFontOfSize:12];
cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[appSmallLogos objectAtIndex:indexPath.row]]]];
cell.detailTextLabel.text=[appReleaseDates objectAtIndex:indexPath.row];
}
return cell;
}

Try this code :
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(your bounds);
[cell addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
//This is what you will load lazily
NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
NSURL *imageURL=url;
NSData *image=[NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image=[UIImage imageWithData:image];
[cell setNeedsLayout];
[indicator stopAnimating];
});
});
cell.cellDescription.text=r.url;// this is which you want to add in first time.
I am not sure but try it.

You can use the SDWebImage library, the library will download and cache the image asynchronously. by passing the url to it as argument
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.textLabel.text = #"My Text";
return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableCellLayout *cell = (TableCellLayout *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[TableCellLayout alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.cellName = [[array valueForKey:#"name"] objectAtIndex:indexPath.row];
cell.cellImage = [UIImage imageNamed:#"placeholder.png"]; // add a placeholder
NSString *imageURL = [[array valueForKey:#"imageLink"] objectAtIndex:indexPath.row];
NSURL *theURL = [NSURL URLWithString:imageURL];
if (asynchronousImageLoader == nil){
asynchronousImageLoader = [[AsynchronousImages alloc] init];
}
[asynchronousImageLoader loadImageFromURL:theURL];
cell.cellImage = asynchronousImageLoader.image;
return cell;
}
I finally managed to do it setting the image with:
– performSelectorOnMainThread:withObject:waitUntilDone:
After the image is downloaded

Related

How to display images in tableview cell? String is not passing to tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"HistoryCell";
//UITableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITableViewCell * Cell=[tblView dequeueReusableCellWithIdentifier:cellIdentifier];
if (Cell == nil)
{
Cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
Cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
myString=[arr objectAtIndex:indexPath.row];
NSLog(#"String %#",myString);
//NSString *newString = [myString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *imageURL =[NSURL URLWithString:myString];
NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(00, 00, 80, 80)];
imageview.image=[UIImage imageWithData:image];
[Cell addSubview:imageview];
//cell.imageView.image=[UIImage imageWithData:image];
return Cell;
}
Please make the following correction:
static NSString *cellIdentifier = #"HistoryCell";
UITableViewCell * Cell=[tblView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
This might be the missing words....

don't want to use dequeueReusableCellWithIdentifier for UITableViewCell

In my UITableViewCells I need to download different image for each cell, what should I do for such kind of problem...
You should use that method still, and then configure each cell independently. Just make sure your cell setup isn't on your construction block
UITableViewCell *cell = [tableView dequeue..];
if (!cell) {
cell = ...;
}
cell.image = ...;
item will be your dictionary of images
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"mainTitleKey"];
cell.detailTextLabel.text = [item objectForKey:#"secondaryTitleKey"];
NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:#"imageKey"] ofType:#"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
return cell;
}

how to set image in uitable view

I download some images using an NSThread. When all the images are downloaded, I have to put them in cell.myimageview. Give me the solution for setting an image in a user-defined method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableCell *cell = (TableCell *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *bedsbaths=[NSString stringWithFormat:#"Beds:%# Baths:%#",[[AppDeleget.statuses valueForKey:#"beds"] objectAtIndex:indexPath.row],[[AppDeleget.statuses valueForKey:#"baths"] objectAtIndex:indexPath.row]];
cell.mlsno.text=[[AppDeleget.statuses valueForKey:#"mlsno"] objectAtIndex:indexPath.row];
cell.price.text=[[AppDeleget.statuses valueForKey:#"price"] objectAtIndex:indexPath.row];
cell.address.text=[[AppDeleget.statuses valueForKey:#"address"] objectAtIndex:indexPath.row];
cell.bedsbaths.text=bedsbaths;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
-(void)LoadImage
{
for(int x=0;x<[ListPhotos count];x++)
{
NSData *imageData =[ListPhotos objectAtIndex:x];
id path = imageData;
NSURL *url = [NSURL URLWithString:path];
NSLog(#"%#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[self performSelectorOnMainThread:#selector(downloadDone:) withObject:img waitUntilDone:NO];
}
}
-(void)downloadDone:(UIImage*)img {
// I have to set the cell here. How?
cell.myimageView.image=img
}
In -(void)LoadImage
Store the img in a NSArray.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , code as
cell.myimageView.image=[imgArray objectAtIndex:indexPath.row];
Assign the delegate and datasource of tableview as,
-(void)downloadDone:(UIImage*)img {
self.tableView.delegate=self;
self.tableView.datasource=self;
}

Empty Table with no data display

I having problem with my table. After loading everything. My data is being loaded and string into name and loadscore. However it will not show in the table. Please help.
static const NSInteger kNameLabelTag = 1337;
static const NSInteger kScoreLabelTag = 5555;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *nameLabel = [[UILabel new] autorelease];
[nameLabel setTag:kNameLabelTag];
[cell.contentView addSubview:nameLabel];
UILabel *highscoreLabel = [[UILabel new] autorelease];
[highscoreLabel setTag:kScoreLabelTag];
[cell.contentView addSubview:highscoreLabel];
}
// Configure the cell.
//cell.textLabel.text = [loadhighScore objectAtIndex:indexPath.row];
NSDictionary *score = [self.loadhighScore objectAtIndex:indexPath.row];
NSString *name = [score objectForKey:#"ScoreName"];
NSString *loadscore = [score objectForKey:#"HighScore"];
[(UILabel *)[cell.contentView viewWithTag:kNameLabelTag] setText:name];
[(UILabel *)[cell.contentView viewWithTag:kScoreLabelTag] setText:loadscore];
return cell;
}
Do you have the tableview delegate and datasource connected to your class?
if you are loading in this data after the view has appeared on screen check that you have reloaded the tables data source [yourTableView reloadData];

use UIWebview in a TableView Cell?

i am trying to load a image in uiwebview in a tableView cell using
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSInteger Index=indexPath.row;
switch (Index)
{
case 0:
[cell.contentView addSubview:webView];
NSString *str=[[NSString alloc]initWithFormat:#"%#",[appDelegate.respSrcUrl objectAtIndex:0]];
NSURL *url = [NSURL URLWithString: str];
NSString *htmlString = #"<html><body><img src='%#' width='900'></body></html>";
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString,url];
webView.scalesPageToFit = YES;
[webView loadHTMLString:imageHTML baseURL:nil];
break;
default:
break;
}
but no image is getting loaded in the webview. how can i display the image in webview??
Make sure you return cell; at the end of the delegate method.