2

I am creating a custom Table by designing my own UITableViewCell in Xcode and loading it in cellForRowAtIndexPath. I am facing scrolling problems; the table scrolls really slow and not smooth at all. In each cell I have:

  • Either 2 or 3 UIImageView where the 3rd one is added dynamically when needed
  • 1 UILabel
  • 1 UITextView
  • Some cells might have 2 UIButton, some 1 UIButton, and the majority no buttons at all
  • In some cells I might add an overlay UIView

The sizes of the image views, text view, and buttons are different in every cell. To prevent calculating the sizes every time the table is scrolled I load all the sizes in viewWillAppear and store them in an array, and inside cellForRowAtIndexPath I only load the sizes from the array and set them to the desired objects.

I tried all the ideas that I found on the web (like loading the images in the body of if(cell == nil) as shown here, or not de-queue the cells...) but nothing solved my problem.

On the other hand I notice that the chatting app WhatsApp uses similar amount of items per cell (example when the user receives an image, there are 2 buttons View and Forward, the image thumbnail, a check mark image next to the thumbnail, a date label, a bubble image behind the thumbnail...) but the scrolling there is very smooth.

My scrolling is slow even if I have 10 cells in the table only. Here are my questions:

  1. Am I doing something dramatically wrong that I am not noticing?
  2. Is there a limit for the number of items that should be added to a custom cell to prevent slow scrolling?
  3. Is there a way to detect the reason of slow scrolling by using something like Product->Profile in Xcode?
  4. Can anyone suggest a solution for my problem?

Many thanks in advance.

Community
  • 1
  • 1
antf
  • 3,162
  • 2
  • 26
  • 33
  • A .xib file created in Xcode, I load it in `cellForRowAtIndexPath` using `loadNibNamed`, and I link to each item in the cell by their tags using `viewWithTag`. – antf Jun 17 '12 at 12:37
  • that is fine, in my code i load more than 100 cell and it works very good – Omar Abdelhafith Jun 17 '12 at 12:51
  • hmm, do you mean that the reason of slow scrolling is not due to the items that I have? And in that case, what should be the slowing reason? – antf Jun 17 '12 at 12:53
  • you will need to add more code, so that we could guess the reason with more accuracy, also run your application with profiler (command + i) and select time profiler, check wich function is taking more time, and post it here so we could help – Omar Abdelhafith Jun 17 '12 at 12:57
  • Sorry for being late, I took some time to find out how to minimize the inspection range.. So, when I scroll my table the Time Profiler showed a big activity, I selected only this part of the statistics and I selected the option Hide System Libraries, the result was the function `cellForRowAtIndexPath` was most used with value 929ms while the first thing after it was 4 ms. What I do there is as I said above, set the sizes of the components from the preloaded array. Suggestions? – antf Jun 17 '12 at 14:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12656/discussion-between-omar-abdelhafith-and-antf) – Omar Abdelhafith Jun 17 '12 at 14:21
  • can you post the code for cellForRowAtIndexPath? – Omar Abdelhafith Jun 17 '12 at 14:27

3 Answers3

1

I had the same problem with multiple image per cell. UITextView is killing it if each cell has one. Also hide images you don't want to show and try to not loop into an array to find if yes or no you should show or hide an image but make an array of BOOL that can be read

Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
  • Thanks for the tips. I have a `UITextView` in every cell, I thought in using a `UILabel` instead but if I do I will not have the ability to detect hyperlinks and phone numbers as the `UITextView` does, right? In fact I only selected text views because of this property. By the way, removing the `UITextView` didn't give me great speed improvement. – antf Jun 17 '12 at 14:24
1

Slow operation of a TableView when using images is NORMALLY because of resizing operations. The last app I made had images coming from the web. as an example, one image that was coming in was a 4k pixel x 4k pixel image. All others were much larger than the thumbnail sized image I was trying to display. Long and short of it is, every time an image goes off screen, the image is moved out of RAM and back into flash, this is to help preserve RAM memory. The problem is that what stays in flash is the original UIImage that you are assigning to your UIImageView. A UIImage retains the original sized image. Thus now every time the image comes back on screen it has to be RESIZED from its original size (smaller or bigger than you wish to display). Every time that 4k px x 4k px image came back on screen, the TableView would glitch. The trick is to size the images to exactly what you wish to display prior to assigning it to the UIImage backing the UIImageView. As an exemplary note, this table view only was displaying ~ 10 to 12 items at a time and it was still extremely slow (even after all images war completely loaded from the web).

Another potential slowness. I had another app I built where I originally wished for the data being displayed to be selectable for copy and past. You can do this with UITextView objects. Problem is that the table of values I was outputting was 17 columns wide by 218 rows. In this case because each UITextView has scroll capability (even when I TURNED IT OFF) was causing each UITextView to have to re-calculated it's own sub-view display coordinates, etc... causing very SLOW overall table scrolling. In the end, if the data doesn't need to be edited, or copy and pasted, you really should only be displaying it with UILabel objects. When I did this, the performance of my UITableView went up immensely.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • 1
    I am using `UITextView` just to be able to detect hyperlinks and phone numbers. Up to my knowledge `UILabel` can't do so, am I right? Also I am not resizing the images but for testing I will try to remove it completely and will tell you what happens in a while. – antf Jun 17 '12 at 14:53
  • True, UILabels will not recognize your URLs etc... In your case of only displaying 10 or so items list items, it shouldnt matter. Your images are most likely the biggest problem. Make Sure they are sized to the output you wish to see!!! – trumpetlicks Jun 17 '12 at 14:55
  • 1
    I've been trying since I asked my question to handle this. Removing the UITextView was the only thing that had an acceptable effect in the speed issue. My images are not re-sized and are small in nature (maximum 200 pixels). After removing the text view the speed is barely acceptable and I will move on for the while. – antf Jun 20 '12 at 17:07
  • Size of your images STILL matters (even though they are small). It is and isnt completely related to the size of the image only. It is truly whether the image needs to be RESIZED for display. for best performance, your actual images should be pre-sized to the pixel size you wish to display on screen. – trumpetlicks Jun 20 '12 at 17:12
0

try this: -webkit-overflow-scrolling: touch; taken from here: http://css-tricks.com/snippets/css/momentum-scrolling-on-ios-overflow-elements/

Ofer Segev
  • 5,094
  • 2
  • 22
  • 22