简体   繁体   English

iOS中的跳过/忽略方法

[英]Skip/ignore method in iOS

How can I skip a complete method in iOS? 如何跳过iOS中的完整方法? I know how to test for the iOS version inside a method, but not how to completely ignore a method. 我知道如何测试iOS版本的方法里面 ,而不是如何完全忽略的方法。

Concrete example: iOS8 added self sizing table view cells, and the methods heightForRowAtIndexPath: and estimatedHeightForRowAtIndexPath: are no longer required. 具体的例子:iOS8上加入自上浆表视图细胞,并且这些方法heightForRowAtIndexPath:estimatedHeightForRowAtIndexPath:不再需要。 But I do need them for iOS7. 但是对于iOS7,我确实需要它们。 Now when I step through the code in iOS8, both methods are called, even though they are no longer needed. 现在,当我逐步浏览iOS8中的代码时,即使不再需要这两种方法,它们也会被调用。

You have a UITableViewDelegate set as the delegate for a UITableView and you want to present different delegate methods to different versions of iOS. 你有一个UITableViewDelegate设置为委托一个UITableView ,你想呈现不同的委托方法不同版本的iOS。

UITableView will call [delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)] before calling [delegate tableView:self heightForRowAtIndexPath:indexPath] . UITableView将在调用[delegate tableView:self heightForRowAtIndexPath:indexPath]之前调用[delegate tableView:self heightForRowAtIndexPath:indexPath] [delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)] [delegate tableView:self heightForRowAtIndexPath:indexPath]

This calls for a custom -respondsToSelector: . 这需要一个自定义-respondsToSelector: In your UITableViewDelegate class add this method. 在您的UITableViewDelegate类中添加此方法。

- (BOOL)respondsToSelector:(SEL)aSelector
{
    // If this device is running iOS 8 or greater.
    if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) {
        if (aSelector == @selector(tableView:heightForRowAtIndexPath:))
            return NO;

        if (aSelector == @selector(tableView:estimatedHeightForRowAtIndexPath:))
            return NO;
    }

    return [super respondsToSelector:aSelector];
}

UPDATE: I fixed the delegate method names. 更新:我修复了委托方法的名称。 DUH! H!

Provide a different delegate based on iOS version. 根据iOS版本提供其他代表。 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. 这使您可以封装你的代码意义命名块(你的界面将表明它是iOS7),你不会做任何弄虚作假respondsToSelector可能会打破你的类的子类,实际上确实想使用这些方法。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM