Reputation: 175
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.
Upvotes: 6
Views: 17592
Reputation: 175
Regarding how to create reference cell (see original question), there are couple of options as I can see:
create a custom cell with XIB, great article here -> a link!
create your custom cell in code, along with constraints.
create your custom cell in storyboard as prototype cell
Thanks everyone for help!
Upvotes: 10
Reputation: 9915
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;
}
Upvotes: 1
Reputation: 40048
You can just dequeue the cell once and store the height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSNumber *height;
if (!height)
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"YourCustomCell"];
height = @(cell.bounds.size.height);
}
return [height floatValue];
}
Upvotes: 0