UITableView在viewDidLoad中间初始化?

时间:2011-10-12 23:15:54

标签: ios uitableview viewdidload

我有一个我正在初始化的VC,其中只有ViewView中有UITableViewUIButtonUIImage

突然之间我正在弄乱按钮属性,wham:

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返回nil并崩溃。

有关为何发生这种情况的任何提示?

以下是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。无法找到AnimationDetailListingCell标识符,因为registerNib中的viewDidLoad代码尚未运行。似乎所有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;
}

1 个答案:

答案 0 :(得分:1)

我有同样的错误。我认为这是因为调整某些按钮属性意味着需要设置视图(显然是懒洋洋地创建),这意味着表需要准备就绪,而且还没有,可能是因为它的数据源尚未连接或者其他的东西。尝试稍后在viewDidLoad方法中将按钮调整移动到以后;这对我有用。