Remove "heightForRowAtIndexPath" for iOS8

1.2k views Asked by At

I'm trying to solve this problem for two days before finding the solution. Here it is.

======

Hi everyone,

I actually manage the height of my tableViewCell in iOS7 with the delegate method heightForRowAtIndexPath:

Now, in iOS8 I use the property tableView.rowHeight = UITableViewAutomaticDimension with autolayout and it works great and better than before.

The problem is that I need to remove the heightForRowAtIndexPath: method from my code, otherwise the property doesn't work (the height is set inside and take control on the property).

How can I do to remove this method in iOS8 only (perhaps with a compilation directive but I don't know which one) ?

I still need it for ios7.

via

3

There are 3 answers

0
Tejas Ardeshna On BEST ANSWER

try this one

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if(IS_IOS8){
     return UITableViewAutomaticDimension;
     }
     else
    {
       int height; // your custom  calculated height
       return height;
     }
 }
0
egarlock On

You can still use tableView:heightForRowAtIndexPath:

- (void)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
0
WMZ On

I had also this problem, so look here:

1) We need to create macro

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

2) Than need to check version in viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (![self.p_tableView respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)])
    {
        self.p_tableView.rowHeight = UITableViewAutomaticDimension;
        self.p_tableView.estimatedRowHeight = 100.0f;
    }
}

3) We need to override respondsToSelector method, this way need's less resources consumption cause of we needn't to call heightForRow for each row

- (BOOL)respondsToSelector:(SEL)selector
{
    static BOOL useSelector;
    static dispatch_once_t predicate = 0;
    dispatch_once(&predicate, ^{
        useSelector = SYSTEM_VERSION_LESS_THAN(@"8.0");
    });

    if (selector == @selector(tableView:heightForRowAtIndexPath:))
    {
        return useSelector;
    }

    return [super respondsToSelector:selector];
}

Hope it helps

via