Requirement : To Display upto 9 Datapoints in a single Custom TableViewCell with 2 UILabels,4 UIButtons and 3 UIImages.
Problem Statement:Scrolling with the Custom UITableViewCell is not smooth with these UIImages and UIButtons added as subviews. There are approximately 500 rows in the UITableView.
If the problem is in number of view on one cell.
Try to set shouldRasterize property of cell's layer to YES.
yourCell.layer.shouldRasterize = YES;
yourCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
But remember that all animated views such as UIActivityIndicatorView will force the cell to redraw it's content on each frame.
I use this Library which is just perfect
SDWebImage
You just need to #import <SDWebImage/UIImageView+WebCache.h> to your project, and you can define also the placeholder when image is being downloaded with just this code:
- (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;
}
It also cache downloaded images and gives you great performance.
Hope it will help you!
Related
In my app I load cell's photo right in cellforrowatindexpath, but it takes main thread for a long period, and when I scroll, my app stops for a while.
What should I do? If I add async thread it would call it every time I scroll and there could be 2 or more equal photo loading requests, and this is not good... Is there any idea without spaghetti coding?
Try SDWebImage framework
https://github.com/rs/SDWebImage
Sample Code
- (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];
}
// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:#"Get each url from array and set it here using indexpath.row"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.textLabel.text = #"My Text";
return cell;
}
It supports variety of features like caching etc.
I have following code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tableId = #"details";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableId];
if(cell == nil)
{
cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableId];
}
[((UIImageView *)[cell viewWithTag:101]) setImage:[UIImage imageNamed:[homeInvCaptureViewModel.productArray objectAtIndex:indexPath.row]]];
return cell;
}
I have a table view in storyboard. Yet, my data is not shown up in my table rows. (i.e., imageview).
I just try your code, and it's working for me, so I see some explanations for your bug:
Did you set 101 for your ImageView's tag in your storyboard?
Did you set details as cell's identifier is in your storyboard?
Are you sure your image's name from your productArray exists in your project's images?
You should set an image at your ImageView in your storyboard to identify,
Hi I have one UItableView with images,title and description
When i run the app i can see the images are showing as large size but if i click that row then images become small(original size ).
May i know why this happening?
code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
offerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.offerImage.layer.masksToBounds=YES;
cell.offerImage.layer.cornerRadius=9.0;
cell.backgroundColor=[UIColor clearColor];
if([offertitle count]>0){
// Configure the cell...
NSLog(#"%#",backupImage);
[cell.offerImage setImageWithURL:[NSURL URLWithString:[backupImage objectAtIndex:indexPath.row]]placeholderImage:[UIImage imageNamed:#"Placeholder.png"]]
;
cell.offerTitle.text=[offertitle objectAtIndex:indexPath.row];
cell.offerDetails.text=[offerDetails objectAtIndex:indexPath.row];
}
// cell.websiteImage.image=[imageArray objectAtIndex:indexPath.row];
return cell;
}
In UitableViewCell, the default image frame will be change based on setting UIImage. I means if you set tableview height as 50, so by default the Default Cell UIImageView height 50. so based on height imageview width will be change for displaying the image without blur.
so you have to take all images are at same size, or You have to create custom UIImageView in place of cell default UIImageview.
I'm trying to display a UITableView with a list of artists who read from Last.fm API. I store all the artists in an array, then I show a table with your name and your picture.
Initially the photos look good, but when I do scroll the images are very small.
This is the initial appearance:
This is the appearance after scrolling, with the problem:
This is my code for create the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TopArtistCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
JSCArtist *artist = self.artists[indexPath.row];
cell.textLabel.text = [artist name];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[artist photo] thumbnail]] placeholderImage: [UIImage imageNamed:#"default_photo.jpeg"]];
return cell;
}
The default imageView for a UITableView acts a little odd. I would just create a custom UITableViewCell subclass and do your own layout.
Hi found the answer here: Images getting mixed up in a UITableView - XML parsing
I'm parsing an XML file with links to images which I'm putting into a UITable. For some reason the pictures are getting completely mixed up when I scroll down in the table. Here's the code I'm using for my UITable:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:customImage];
}
NSString *picURL = [currentTweet pic];
if (![picURL hasPrefix:#"http:"]) {
picURL = [#"http:" stringByAppendingString:picURL];
}
customImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:picURL]]];
return cell;
}
Any idea what I'm doing wrong? Any help is seriously appreciated. Thx!
Its because you are dequeueing reusable cells. It is recycling a cell that had a previously used image in it.
I noticed that you are setting the image in the same if block that you are initializing the cell. If I were you I'd move [cell.contentView addSubview:customImage]; to right before your return statement. This way when it recycles a dequeued cell, it won't have an image in it.
Are the images being downloaded from the web?
If so then the delay in downloading the images will be causing this. You'll need to do it in a multi threaded way.
Take a look at this example...
Question about Apple's LazyTableImages sample - Doesn't behave exactly like the app store
There are also many tutorials online about lazy loading with images and UITableViews.