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

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

问题描述

我已经通过使用为 iOS 8 实现了自动动态 tableview 单元格高度

I've implemented automatic dynamic tableview cell heights for iOS 8 by using

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];
}

这样,当table view调用时:

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

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

您的代码在 iOS 8 或更高版本下将返回 NO,在 iOS 7 或更早版本下返回 YES.

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

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

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