断言失败 – ,/ SourceCache /UIKit_Sim/UIKit-2903.2/UITableView.m:7768

我试图显示与UIToolBar UIPickerView但出现一些错误。

这是我的代码 –

CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44); CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216); UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds]; darkView.alpha = 0; darkView.backgroundColor = [UIColor blackColor]; darkView.tag = 9; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)]; [darkView addGestureRecognizer:tapGesture]; [self.view addSubview:darkView]; UIDatePicker *picker = [[UIDatePicker alloc] init]; picker.autoresizingMask = UIViewAutoresizingFlexibleWidth; picker.datePickerMode = UIDatePickerModeDate; [picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged]; [picker setFrame:CGRectMake(0,235,320,120)]; picker.backgroundColor = [UIColor blackColor]; [self.view addSubview:picker]; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)]; toolBar.tag = 11; toolBar.barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)]; [toolBar setItems:@[spacer, doneButton]]; [self.view addSubview:toolBar]; [UIView beginAnimations:@"MoveIn" context:nil]; toolBar.frame = toolbarTargetFrame; picker.frame = datePickerTargetFrame; darkView.alpha = 0.5; [UIView commitAnimations]; 

在这一行上得到错误 –

 picker.frame = datePickerTargetFrame; 

这是错误 –

 *** Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:7768 2013-10-03 13:43:12.688 Mistoh Beta 1[7228:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource is not set' libc++abi.dylib: terminating with uncaught exception of type NSException 

请帮助我,请提前感谢您。

我有同样的问题,这是从iOS7.03开始出现的一些崩溃。 它可以通过移动[self.view addSubview:picker]来解决; 在你的例程结束后,基本上在picker.frame = datePickerTargetFrame;

[self.view addSubview:picker]; 必须在所有的拾取器操作之后添加

我正在使用UIDatePicker作为inputView,所以Tao-Nhan接受的答案不适合我。 我一直在苦苦挣扎,但是今天我终于find了一个优雅的解决方法!

经过多次调查,我发现碰撞发生在input视图上调用didMoveToSuperview之后。 诀窍是将UIDatePicker作为子视图的“包装器”视图作为inputView使用,并将input视图从超级视图中移除,并将其移动到runloop上的下一次运行时重新添加一个新的超级观点。 如果这听起来太混乱,只需使用下面的代码作为input视图,你会没事的。

TL; DR使用UIDatePicker作为inputView? 这是我find的解决方法:

GSDatePickerInputView.h

 #import <UIKit/UIKit.h> @interface GSDatePickerInputView : UIView @property (nonatomic, readonly) UIDatePicker *datePicker; @property (nonatomic) BOOL useWorkaroundToAvoidCrash; @end 

GSDatePickerInputView.m

 #import "GSDatePickerInputView.h" @interface GSDatePickerInputView () @property (nonatomic, strong, readwrite) UIDatePicker *datePicker; @end @implementation GSDatePickerInputView - (instancetype)init { if (self = [super initWithFrame:CGRectMake(0, 0, 320, 166)]) { self.translatesAutoresizingMaskIntoConstraints = NO; self.backgroundColor = [UIColor whiteColor]; UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601]; datePicker.backgroundColor = [UIColor whiteColor]; [self addSubview:datePicker]; self.datePicker = datePicker; } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.datePicker.frame = self.bounds; } - (void)willMoveToSuperview:(UIView *)newSuperview { if (self.useWorkaroundToAvoidCrash == YES) { if (newSuperview == nil) { [self.datePicker removeFromSuperview]; } } } - (void)didMoveToSuperview { if (self.useWorkaroundToAvoidCrash == YES) { if (self.superview != nil) { dispatch_async(dispatch_get_main_queue(), ^{ [self addSubview:self.datePicker]; self.datePicker.frame = self.bounds; }); } } } @end 

关键部分是方法willMoveToSuperview: didMoveToSuperviewdispatch_async GCD函数用于在发生崩溃后将datePicker返回。

然后你可以像这样使用这个inputView的一个实例:

 GSDatePickerInputView *dateInputView = [[GSDatePickerInputView alloc] init]; dateInputView.useWorkaroundToAvoidCrash = YES; [dateInputView.datePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged]; yourView.inputView = dateInputView; 

您可以稍后使用此代码访问datePicker:

 ((GSDatePickerInputView *)yourView.inputView).datePicker 

最后一个注意事项 – 属性useWorkaroundToAvoidCrash是在一个地方崩溃,但在另一个地方它不是(发生在我身上)的情况。 只要有可能就避免这样的黑客事件显然是更好的,所以只有在实际崩溃的地方将此属性设置为YES。

我有一个类似的问题,我不得不改变maximumDateminimumDateUITableView的许多UITextField的一个input视图的子视图,经过大量的跟踪,似乎问题是设置两个date在同一时间。 我能够得到这个工作的唯一方法是从我的自定义input视图中完全删除UIDatePicker ,并创build并添加它,并设置新的最小和最大date值。 这是我在相当一段时间里被迫做的最丑陋的事情。

UIDatePicker在内部pipe理一个UIPicker。 所以,这个消息里面包含了UIPicker。 现在到实际的错误信息:当你试图显示DatePicker时,你有足够的空间吗? 它会抛出这个错误的原因之一是如果找不到足够的房地产来显示整个视图。

UIPickerView在自定义的UITableViewCell中有同样的问题。 我已经移动了所有处理Picker视图放在父视图上的逻辑加上它的约束设置- (void)layoutSubviews

更新后,看起来像下一个方法:

 - (void)layoutSubviews { [super layoutSubviews]; [self addSubview:YOUR_PICKER_VIEW]; //'self' in my case was UITableViewCell [self addConstraints:PICKER_VIEW_CONSTRAINTS]; } 

我有类似的崩溃。 我把我的代码在dispatch_after和崩溃解决。

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (textField == _txtFieldEndDate) { _datePicker.maximumDate = [NSDate dateWithDaysFromNow:100 * 365]; [_datePicker setDate:[NSDate date]]; } else if (textField == _txtFieldStrtDate) { _datePicker.maximumDate = [NSDate date]; } });