Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load custom UITableViewCell from Storyboard

Currently I'm using auto layout with storyboard to dynamically resize custom UITableViewCell's. Everything is working as it should except there is memory leak when scrolling.

I know the problem is from calling

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

from inside

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

My question is this, what would be the best way to create a reference cell? How to load a cell from storyboard without dequeueReusableCellWithIdentifier?

And is it ok to maybe call dequeueReusableCellWithIdentifier from viewDidLoad and create a reference cell as a property?

I need reference cell for sizing purposes.

Thanks for any help.

like image 798
Iki Avatar asked Apr 06 '14 20:04

Iki


2 Answers

Regarding how to create reference cell (see original question), there are couple of options as I can see:

  1. create a custom cell with XIB, great article here -> a link!

    • this was pointed out to me by @smileyborg, thanks!
  2. create your custom cell in code, along with constraints.

  3. create your custom cell in storyboard as prototype cell

    • after that there are 2 options:
      • drag and drop custom cell from table view to the storyboard scene, as a top level nib object (not part of the view hierarchy), and simply create outlet to it.
      • copy custom cell and paste it to the storyboard scene. When using it this way, there is no need to register original cell with table for dequeue.
      • minor problem with both approaches is that once you drag and drop custom cell to the storyboard scene, you will not be able to see it visually and arrange UI.
    • great article here -> a link!

Thanks everyone for help!

like image 108
Iki Avatar answered Oct 16 '22 23:10

Iki


And is it ok to maybe call dequeueReusableCellWithIdentifier from viewDidLoad and create a reference cell as a property?

Yes, you can do this. Or lazy initialize the property:

- (UITableViewCell *)referenceCell
{
    if (!_referenceCell) {
        _referenceCell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
    }
    return _referenceCell;
}
like image 34
Timothy Moose Avatar answered Oct 17 '22 00:10

Timothy Moose