如何根据运行时系统iOS版本覆盖方法? [英] How to only override a method depending on the runtime system iOS version?

查看:191
本文介绍了如何根据运行时系统iOS版本覆盖方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

self.tableView.rowHeight = UITableViewAutomaticDimension;

对于不支持自动动态单元格高度的iOS 8之前的版本,我重写了heightForRowAtIndexPath方法。

For pre-iOS 8, which does not support automatic dynamic cell heights, I overrided the heightForRowAtIndexPath method.

这与我的做法类似:在UITableView中使用自动布局进行动态单元格布局&可变行高

问题在于如何编写使用iOS 8自动单元格高度的代码,但是为早期iOS版本覆盖heightForRowAtIndexPath。
我只想在iOS版本小于8的情况下使用我的自定义heightForRowAtIndexPath方法。
有关如何执行此操作的任何建议吗?

The problem is to how to write code that uses automatic cell height for iOS 8 but overrides heightForRowAtIndexPath for earlier iOS versions. I'd like my custom heightForRowAtIndexPath method only if iOS version is less than 8. Any suggestions on how to do this?

推荐答案

一种解决方案是覆盖视图控制器中的 respondsToSelector:方法。检查 heightForRowAtIndexPath:方法时,让它在iOS 8下返回 NO

One solution would be to override the respondsToSelector: method in your view controller. Have it return NO under iOS 8 when checking for the heightForRowAtIndexPath: method.

- (BOOL)respondsToSelector:(SEL)selector {
    static BOOL useSelector;
    static dispatch_once_t predicate = 0;
    dispatch_once(&predicate, ^{
        useSelector = [[UIDevice currentDevice].systemVersion floatValue] < 8.0 ? YES : NO;
    });

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

    return [super respondsToSelector:selector];
}

这样,当表格视图发出如下调用时:

This way, when the table view make a call like:

if ([self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)]) {
}

您的代码将在iOS 8或更高版本及<$ c下返回 iOS 7或更早版本中的$ c> YES 。

your code will return NO under iOS 8 or later and YES under iOS 7 or earlier.

这篇关于如何根据运行时系统iOS版本覆盖方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆