Weird UITableViewCell Loading Exception

2020-05-23 03:43发布

问题:

In my app, I use a custom NIB to load my UITableViewCells. The NIB's File's Owner is set so the class is my View Controller. I then Link the UITableViewCell to the IBOutlet I put in the header file. It was all working fine, until all of the sudden it stopped working, and gets this error:

uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (UITableViewCellContentView)'

I have traced this exception to [NSBundle loadNibNamed:owner:options:]

static NSString *CellIdentifier = @"SubjectCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"SubjectCell" owner:self options:NULL];
cell = customSubjectCell;

I have tried this on two devices and the simulator, but all get the same error. I downloaded the new SDK today, and I think that may have caused this.


PLEASE NOTE:

This question is outdated, and the fix below was a temporary fix. Apple has fixed its SDK, so if you have the newest version of the SDK this does not apply to you.

回答1:

Found solution in dev forums

Create this files:

UITableViewCellContentView.h

#import <UIKit/UIKit.h>
@interface UITableViewCellContentView : UIView {
}
@end

UITableViewCellContentView.m

#include "UITableViewCellContentView.h"
@implementation UITableViewCellContentView

+ (id)alloc {
    return [UIView alloc];
}

+ (id)allocWithZone:(NSZone *)zone {
    return [UIView allocWithZone:zone];
}

@end


回答2:

Uh oh. I'm having the same issue... Also using today's new SDK (01/28/2010). Can't say for sure whether the code was working before, because I just added it AFTER updating to the new SDK.

Anybody else seeing this issue now? Any solutions?

Update: I just switched the simulator to SDK 3.2 (from 3.12), and whattaya know... it's working now (in the iPhone and iPad simulator). However, it doesn't work when compiling using SDK 3.12.

My guess is that IB is saving the XIB in a new format for 3.2 perhaps? Anybody know how to force it to save in the old (3.12) format? This definitely feels like a bug, as it's breaking previously working code.



回答3:

I had the same thing. Installed 3.2 beta from 3.1.2 and had this error.

In the end this fixed it: Rebuild against Simulator 3.1 and I got a working build in the simulator. After this, builds against Simulator 3.1.2 work.