4

I have a class that inherits from UITableViewCell, it has a bunch of IBOutlets. I had previously been using this object in a way that reuses the cell and initializes it as it's needed. This method is too slow, so I decided to create an array of the UITableViewCell objects and then add them as needed in the cellForRowAtIndexPath: method.

Everything gets loaded fine except the IBOutlet objects. awakeFromNib is never called so I assume this has something to do with my issue.

Just to clarify it was getting called fine when I was initializing the cells in the cellForRowAtIndexPath function, it's just when I tried to preload them in the view controllers viewWillAppear method that it breaks.

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
Tiddly
  • 1,620
  • 3
  • 21
  • 43
  • 1
    Whatever you're up to I suggest you stop and have a good read through the `UITableView` documentation. "create an array of the UITableViewCell objects and then add them as needed in the cellForRowAtIndexPath: function." really isn't the way to go about things. – Mike Pollard May 29 '13 at 10:53
  • 1
    Can you possibly explain why not? My table only has less than 10 objects in it so preloading them up front shouldn't be a big issue. – Tiddly May 29 '13 at 10:55
  • It's just not the way the `UITableView` is meant to work. I find it a little strange that the regular way of using the table view is too slow. I suggest that you use a profiler to find what's really taking up time. – Vegar May 29 '13 at 10:58
  • http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10 – Mike Pollard May 29 '13 at 10:59
  • I'm sorry but linking to a a page that says how to create a UITableView isn't exactly helpful. As I said in the post I have successfully created one the standard way (as laid out in that link) and it works fine. What I'm trying to do now is preload them, why is that a bad thing? The only thing it's not currently doing is linking to the nib file, everything else works fine it's laid out correctly and it scrolls fine and I can select the individual cells. Just none of the buttons or labels that I defined in the storyboard. – Tiddly May 29 '13 at 11:08
  • I have profiled using instruments, the reason it's so slow is that it's creating a new cell every time it goes on and off screen. The cell contains images and labels and buttons. If it's possible the simplest solution would be to not create a new cell every time it goes off screen and cache them instead, which is what I'm trying to do. – Tiddly May 29 '13 at 11:10
  • Just to clarify, I am not saying that you are wrong and that it's not a bad thing. I would just like to know why? – Tiddly May 29 '13 at 11:20
  • In your comment above, you say it's not linking to the nib file, but then you say you defined the subviews in a storyboard. Where did you make this cell, nib or storyboard? Where do you images come from -- are they local, or are you downloading them from somewhere? – rdelmar May 29 '13 at 14:46
  • Sorry the nib and storyboard were the same thing. I create the table view and cell in the storyboard. The images are all local. – Tiddly May 29 '13 at 15:49

2 Answers2

0

I know this is an old question. But after reading the conversation, I feel I must give some input. This a multi-layered issue that you are dealing with here.

Firstly, when you say "preload" what is it that you mean exactly.? Are you calling dequeueReusableCellWithIdentifier in your viewWillAppear? Or are you calling an init. Either way this is not an acceptable practice whatsoever.

Remember, the UITableViewCells are "Reusable" meaning that the UITableView, in effect, handles the unloading and loading of the UITableViewCells when they are offscreen to optimize performance (not memory, believe it or not) as well as some other things. Essentially, it's a pretty nifty "hack" to keep table views efficient. So if your UITableView is too slow, then you are doing something wrong, not the implementation of UITableView

When you say you are "preloading" them, that throws up some immediate red flags on your usage. And when you say "the method is too slow", well it's not. The delegate/datasource methods in place for UITableViews and UICollectionViews happen in calculated orders. Don't fight it.

Secondly, for your issue with awakeFromNib not getting called. You aren't providing enough information. How did you initialize it? If you created it in your storyboard/nib, you shouldn't be calling any of the init methods. In fact, as of iOS6 you are guaranteed a non-nil cell from dequeueReusableCellWithIdentifier.

taylorcressy
  • 966
  • 8
  • 23
-1

Have you called it through its super class like :

-(void)awakeFromNib
{
    [super awakeFromNib]; // Use this line 
}

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • I have it called just like that. It works when it's loaded on the fly but doesn't get called when I try to preload it and store it in an array. – Tiddly May 29 '13 at 10:54