我有一個VC,我正在初始化,並在其中是一個View
。在View
是UITableView
,UIButton
和UIImage
。UITableView在viewDidLoad中獲得初始化?
我與按鈕搞亂屬性時,忽然,砰:
Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:]
進一步挖掘後,那是因爲我的代碼:
UINib *animCellNib = [UINib nibWithNibName:@"IoUISEAnimationDetailListingCell" bundle:nil];
[animationDetailsTableView registerNib:animCellNib forCellReuseIdentifier:@"AnimationDetailListingCell"];
沒有得到執行。在我的viewDidLoad
方法的頂部顯示了進一步的挖掘,它跳轉到初始化tableView
。因此,從我的方法名稱跟蹤中,可以看到它以numberOfSectionsInTableView
開頭。雖然這只是發生了幾行到我viewDidLoad
2011-10-12 15:43:42.113 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - initWithNibName:bundle:
2011-10-12 15:43:42.116 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - viewDidLoad
Current language: auto; currently objective-c
2011-10-12 15:43:49.017 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - numberOfSectionsInTableView:
2011-10-12 15:43:49.017 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - tableView:titleForHeaderInSection:
2011-10-12 15:43:49.017 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - tableView:titleForFooterInSection:
2011-10-12 15:43:49.017 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - tableView:numberOfRowsInSection:
2011-10-12 15:43:49.017 io[5052:11903] <IoUISEAnimationDetailsViewController: 0x764c460> - tableView:cellForRowAtIndexPath:
2011-10-12 15:43:49.017 io[5052:11903] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072
2011-10-12 15:43:49.018 io[5052:11903] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
在viewDidLoad
方法,一段時間後我設置saveButton.titleLabel.lineBreakMode
和saveButton setBackgroundImage:newImage
之前跳轉到numberOfSectionsInTableView
方法。
由於我的registerNib: forCellReuseIdentifier:
代碼位於按鈕代碼之後,因此單元筆尖未註冊,因此導致我的dequeueReusableCellWithIdentifier
返回零和崩潰。
爲什麼發生這種情況的任何提示?
這裏是viewDidLoad
的代碼:
- (void)viewDidLoad {
if (IoUIDebug & IoUIDebugSelectorNames) {
NSLog(@"%@ - %@", [self description], NSStringFromSelector(_cmd));
}
[super viewDidLoad];
// Create an Empty Tableview and steal its Background.
//set our background to it
UITableView *tv = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
UIView *backgroundView = tv.backgroundView;
[self.view addSubview:backgroundView];
[tv release];
// CGRect backgroundFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//[backgroundView setFrame:backgroundFrame];
[self.view sendSubviewToBack:backgroundView];
saveButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
saveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
saveButton.titleLabel.textAlignment = UITextAlignmentCenter;
saveButton.titleLabel.lineBreakMode = UILineBreakModeCharacterWrap;
[saveButton setTitle:NSLocalizedStringFromTable(@"Save Animation Label",@"ScreenEditor",@"Save Animation Label") forState:UIControlStateNormal];
[saveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
saveButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
UIImage *newImage = [[UIImage imageNamed:@"BlueButtonSmall.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[saveButton setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [[UIImage imageNamed:@"BlueButtonSmallPressed.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[saveButton setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
// in case the parent view draws with a custom color or gradient, use a transparent color
saveButton.backgroundColor = [UIColor clearColor];
if (UIAppDelegate.ioMainViewController.currentDeployment.readOnlyMode == NO) { // Only setup for the long press if the deployment is editable..
instructionLabel.text = NSLocalizedStringFromTable(@"Animation Details Instructions", @"ScreenEditor", @"");
} else {
instructionLabel.text = NSLocalizedStringFromTable(@"Locked Message", @"Configuration",@"");
}
IoCDElementAnimation * currentAnimation = UIAppDelegate.ioMainViewController.currentElementAnimation;
if (currentAnimation) {
newAnimation = currentAnimation;
selectedSysCDAnimation = currentAnimation.sysAnimation;
selectedIoCDTag = currentAnimation.tag;
animationSaved = YES;
} else {
selectedSysCDAnimation = nil;
selectedIoCDTag = nil;
animationSaved = NO;
}
UINib *animCellNib = [UINib nibWithNibName:@"IoUISEAnimationDetailListingCell" bundle:nil];
[animationDetailsTableView registerNib:animCellNib forCellReuseIdentifier:@"AnimationDetailListingCell"];
// set our TableView Background to clear so we can see our new background
[animationDetailsTableView setBackgroundView:nil];
[animationDetailsTableView setBackgroundColor:[UIColor clearColor]];
[self refreshContents:nil];
[self setupOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
[self.view setNeedsLayout];
}
在該方法中,dequeueReusableCellWithIdentifier:
返回nil。由於viewDidLoad
中的registerNib
代碼尚未運行,因此無法找到AnimationDetailListingCell標識符。看起來好像所有viewDidLoad
都應該在發生初始化tableview之前運行,對吧?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (IoUIDebug & IoUIDebugSelectorNames) {
NSLog(@"%@ - %@", [self description], NSStringFromSelector(_cmd));
}
UITableViewCell *cell;
cell = [animationDetailsTableView dequeueReusableCellWithIdentifier:@"AnimationDetailListingCell"];
// cell at this point in nil!
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
我也遇到了這個錯誤,當我跟蹤它時,這是因爲在我的CellForRowAtIndexPath中有一個情況,我可以返回零。 – Sylem