So, it seems that heightForRowAtIndexPath
is called before cellForRowAtIndexPath
. I'm currently using cellForRowAtIndexPath
to calculate the height of each cell (they're variable heights, using a UILabel
with varying amounts of text in it.
I'm puzzled as to exactly how I can set the cell's height correctly if the method that sets the height is called before the method that calculates it.
I've created a category of NSString
, and another of UILabel
, which working together make the UILabel
the right size. The issue is that the UITableViewCell
instances which contain those labels aren't being resized, so the labels hang over the end of each label, overlapping the ones beneath.
I know that what I need to do is make the height of the cell equal the height of the label, plus some extra margin space, but I'm puzzled as to how I can set the height before the label height has been calculated. Can I set the height someone without using heightForRowAtIndexPath
? Or if not, could I calculate the height of the cell elsewhere? I'm currently using indexPath.row
to make that happen, so doing it beforehand would be tricky. Here's the code my cellForRowAtIndexPath:
cellForRowAtIndexPath
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
if (_customCell == nil) {
_customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
}
_customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
_customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];
UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
backView.backgroundColor = [UIColor clearColor];
_customCell.backgroundView = backView;
_customCell.myLabel.frame = CGRectMake(5, 5, 265, 65);
[_customCell.myLabel sizeToFitMultipleLines];
return _customCell;
}