断言失败 - [UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:],/ SourceCache / UIKit_Sim / UIKit-2903.2 /UITableView.m:7768

问题描述 投票:18回答:6

我试图用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

请帮帮我。提前谢谢你。

ios uitableview uiview uipickerview
6个回答
17
投票

我遇到了同样的问题,从iOS7.03开始出现了一些崩溃。它可以通过在例行程序结束时移动[self.view addSubview:picker];来解决,基本上是在设置了选择器的框架之后。即picker.frame = datePickerTargetFrame;

所有采摘操纵后必须添加[self.view addSubview:picker];


5
投票

我使用UIDatePicker作为inputView,所以Tao-Nhan接受的答案对我不起作用。我很长一段时间一直在努力解决这个问题,但今天我终于找到了一个优雅的解决方案!

经过大量调查后,我发现崩溃发生在输入视图上调用didMoveToSuperview之后。诀窍是使用带有UIDatePicker的“包装”视图作为子视图作为inputView,并在从superview中删除inputView时删除选择器,并在将其移动到runloop上的下一次运行后重新添加它一个新的超级视图。如果这听起来太混乱,只需使用下面的代码作为您的输入视图,你会没事的。

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

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是在那里的情况下,在一个地方它崩溃但在另一个地方它不是(发生在我身上)。尽可能避免这种hackery更好,所以只有在它实际崩溃的地方才将此属性设置为YES。


4
投票

我有一个类似的问题,我不得不改变maximumDate上的minimumDateUIDatePicker,这是我的inputView中许多UITextField之一的UITableView的子视图,经过大量跟踪后,似乎问题是设置两个日期在同时。我能够完成这项工作的唯一方法是从我的自定义输入视图中完全删除UIDatePicker并创建并重新添加它并设置新的最小和最大日期值。这是我在很长一段时间内被迫做的最丑陋的事情。


3
投票

UIDatePicker在内部管理UIPicker。因此,消息中包含UIPicker。现在来看实际的错误信息:当你试图显示DatePicker时,你有足够的空间吗?抛出此错误的原因之一是,如果找不到足够的空间来显示整个视图。


2
投票

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];
}

0
投票

我有类似的崩溃。我将我的代码放入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];
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.