本文介绍了iOS 中的跳过/忽略方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何跳过 iOS 中的完整方法?我知道如何在一个方法中inside测试 iOS 版本,但不知道如何完全忽略一个方法.

How can I skip a complete method in iOS? I know how to test for the iOS version inside a method, but not how to completely ignore a method.

具体示例:iOS8 增加了表格视图单元格的自适应大小,不再需要heightForRowAtIndexPath:estimatedHeightForRowAtIndexPath: 方法.但我确实需要它们用于 iOS7.现在,当我在 iOS8 中逐步执行代码时,即使不再需要它们,也会调用这两个方法.

Concrete example: iOS8 added self sizing table view cells, and the methods heightForRowAtIndexPath: and estimatedHeightForRowAtIndexPath: are no longer required. But I do need them for iOS7. Now when I step through the code in iOS8, both methods are called, even though they are no longer needed.

推荐答案

根据 iOS 版本提供不同的委托.这允许您将代码封装在有意义命名的块中(您的界面将表明它是 iOS7)并且您不会对 respondsToSelector 做任何可能破坏您的类的子类的技巧确实想使用这些方法.

Provide a different delegate based on iOS version. This allows you to encapsulate your code in meaningfully-named blocks (your interface will indicate that it's iOS7) and you won't be doing any trickery with respondsToSelector that could break subclasses of your class that actually do want to use those methods.

@interface MyTableViewDelegate : NSObject <UITableViewDelegate>
@end

@interface MyTableViewDelegateiOS7 : MyTableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
@end

@implementation YourClass : .. <..>
// ..
- (void)loadView {
  [super loadView];
  if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
    self.tableView.delegate = [[MyTableViewDelegate alloc] init];
  } else {
    self.tableView.delegate = [[MyTableViewDelegateiOS7 alloc] init];
  }
}
@end

这篇关于iOS 中的跳过/忽略方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:04