Wednesday, November 27, 2013

How to avoid slowdowns when scrolling table view? http://stackoverflow.com/questions/6707681/how-to-avoid-slowdowns-when-scrolling-table-view

http://stackoverflow.com/questions/6707681/how-to-avoid-slowdowns-when-scrolling-table-view

How to avoid slowdowns when scrolling table view?


I have custom table view cell with images (loaded from app document directory), labels, shadows etc. and when I scroll the table view it causes a lot of lags. How can I avoid these lags? I think it's possible to cache table view cell, or get a picture of the table view cell, but I don't know how to implement this. Please, help me :)

In cellForRowAtIndexPath: I set the data in if (cell == nil) block, but slowdowns are still there.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {      static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];        if (cell == nil) {          // ...configuring is here...      }        return cell;  }

P.S. Images in cell are high resolution, but reduced to fit...

asked Jul 15 '11 at 13:26
Randex
19413
add comment

3 Answers

You should load your images in a background thread. If you're on iOS 4+, you can use GCD (Grand Central Dispatch) to load these asynchronously.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath      static NSString *CellIdentifier = @"ImageCell";      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];      if (cell == nil) {          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault                                         reuseIdentifier:CellIdentifier] autorelease];      }      NSString *imagepath = //get path to image here      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);      dispatch_async(queue, ^{          UIImage *image = [UIImage imageWithContentsOfFile:imagePath];          dispatch_sync(dispatch_get_main_queue(), ^{              [[cell imageView] setImage:image];              [cell setNeedsLayout];          });      });      return cell;  }
answered Jul 15 '11 at 14:03
sudo rm -rf
16.6k1055110
 
I am facing the same issue with UITableViewCells. I have already reduced image size according to UIImageView frame and stored in documents directory. When i used your code, there is an increase in performance, but still the scrolling is not smooth. I tried commenting only setimage method and it scrolls fine even if imagewithcontentsoffile exists on the code. Any suggestion is appreciated –  Anil Sivadas Apr 17 '12 at 7:27
 
You can always use a placeholder image for your cells that loads the real image only when scrolling has stopped, if you're extremely worried about performance. Also, here are some great optimizing tips.fieryrobot.com/blog/2008/10/01/… fieryrobot.com/blog/2008/10/08/… You can use SDWebImage to help you use a placeholder. Scroll down on this guide here to see an example: highoncoding.com/Articles/… – sudo rm -rf Apr 17 '12 at 14:02
 
Thanks a lot for the suggestions.. i had 5 images in each row and that made the issue. when i changed the image format to JPG from PNG for the images, loading was much faster and i used some performance tweaks from your reference. –  Anil Sivadas Apr 18 '12 at 15:27
 
Oh yes, several images per row would definitely destroy performance. Good luck with your app! –  sudo rm -rfApr 18 '12 at 15:35
 
This is an excellent solution. I managed to load my entire facebook friendlist (500+ items) pictures, names, ids etc in custom cells and it doesn't lag at all. thanks! –  ABros Jul 8 '12 at 7:50
show 1 more comment

I think you are loading images from URL thats why you are facing lag in scrolling.. you should use Lazy table images check out this -http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html

have a look into above sample code and post your exp. in comments

update 1 - try to remove elements from cell one by one.. and check which element is cause this ..

update 2 - ok.. then you know images are causing issues.. so you need to load them using a thread or you can also use lazy table images concept.. seems image size is quite high.

answered Jul 15 '11 at 13:30
Saurabh
12.5k54487
 
No, I load images from app document directory (NSDocumentDirectory) –  Randex Jul 15 '11 at 13:31
 
check update 1 in answer –  Saurabh Jul 15 '11 at 13:32
 
So, I did it and... yes, images is cause slowdowns... –  Randex Jul 15 '11 at 13:40
 
check updated answer –  Saurabh Jul 15 '11 at 13:49

No comments:

Post a Comment