Refactoring UITableView delegates for iOS7 and iOS

2019-09-26 06:55发布

问题:

As a follow up to this question: Skip/ignore method in iOS, I'm trying to implement separate delegates for my UITableView in iOS7 and iOS8.

So, as a first step, in viewDidLoad of my MyTableViewController, I added the following code:

if ([[[UIDevice currentDevice] systemVersion] compare: @"8.0" options: NSNumericSearch] != NSOrderedAscending)
{
    [self.tableView setDelegate: [[MyTVDelegate alloc] initWithIdentifier: myTVCellIdentifier]];
}
else
{
    [self.tableView setDelegate: [[MyTVDelegate7 alloc] initWithIdentifier: myTVCellIdentifier]];
}

I'm adding an identifier, since I will have to apply this to multiple view controllers (or I may have just create a delegate class for each TV, I haven't figured that out yet).

I'm using CoreData, so my dataSource is an NSFetchedResultsController.

Then, I have the following for MyTVDelegate/myTVDelegate7:

#import "MyTVDelegate.h"

@implementation MyTVDelegate

- (instancetype)initWithIdentifier: (NSString *) identifier
{
    if ([super init])
    {
        self.identifier = identifier;
    }

    return self;
}

@end

@implementation MyTVDelegate7

- (CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView: (UITableView *)tableView estimatedHeightForRowAtIndexPath: (NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

@end

If I run this, I'm getting the following runtime error in iOS7:

2015-01-18 10:42:51.894  -[__NSArrayI tableView:estimatedHeightForRowAtIndexPath:]: unrecognized selector sent to instance 0x7b9dd220
2015-01-18 10:42:57.731  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI tableView:estimatedHeightForRowAtIndexPath:]: unrecognized selector sent to instance 0x7b9dd220'

on iOS8, there is no crash.

The instance 0x7b9dd220 is an NSArray. My hunch is that it crashes, because the indexPath is invalid because the delegate and 'dataSource' are now separate?

I've tried moving the call to performFetch to either before or after setting the delegate, but I get the same error.

How do I fix this, should I for instance move all the NSFetchedResultsController code to the new delegate class as well?

回答1:

self.tableView setDelegate: assigns a weak reference; if you don't hold your own reference to this object, it will get collected. This is why you're seeing the crash. The system has collected the memory that was assigned to your delegate, then reassigned the memory to an NSArray. Your table tries to call methods on the delegate and can't because NSArray does not respond to them.

Alongside the self.tableView property definition, define another property:

@property (strong) id<UITableViewDelegate> myTableViewDelegate;