I have a table view in which the cells are subclassed. In the subclass I call a subclass of UIView.
I have implemented tableView:heightForRowAtIndexPath: and would like to retrieve this height in my subclass to set the height of the instance of my subclassed UIView.
Is there any way to do this?
I have tried using self.bounds.size.height and self.contentView.frame.size.height but none of these seem to give the correct height.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
textCellView = [[TextCellView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
[[self contentView] addSubview:textCellView];
}
return self;
}
At the time it is created the subview has not be positioned, and so you cannot rely on the frame. Instead, try passing the value into your init method on the custom table cell. Nothing says you have to use the initWithStyle:reuseIdentifier: method. Here's an example:
- (id) initWithHeight: (CGFloat) cellHeight {
if ((self = [super initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: @"MyCustomTableViewCell"])) {
// perform all layout that needs to happen
// use cellHeight in all the layout calculations
}
return self;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With