我在UIDatePicker
内有UIViewController
,
在一些罕见的情况下,我得到了这个例外:
Fatal Exception: NSInternalInconsistencyException
UITableView dataSource is not set
0 CoreFoundation 0x1820badb0 __exceptionPreprocess
1 libobjc.A.dylib 0x18171ff80 objc_exception_throw
2 CoreFoundation 0x1820bac80 +[NSException raise:format:]
3 Foundation 0x182a40154 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4 UIKit 0x18756efe0 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
5 UIKit 0x18756f198 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]
6 UIKit 0x18755e468 -[UITableView _updateVisibleCellsNow:isRecursive:]
7 UIKit 0x187573c64 -[UITableView _performWithCachedTraitCollection:]
8 UIKit 0x1873048c4 -[UITableView layoutSubviews]
9 UIKit 0x18794aa38 __35-[UIPickerTableView layoutSubviews]_block_invoke
10 UIKit 0x1872275b4 +[UIView(Animation) performWithoutAnimation:]
11 UIKit 0x18794a9c4 -[UIPickerTableView layoutSubviews]
12 UIKit 0x1872141e4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
13 QuartzCore 0x184ba6994 -[CALayer layoutSublayers]
14 QuartzCore 0x184ba15d0 CA::Layer::layout_if_needed(CA::Transaction*)
这是UIViewController的代码:
import UIKit
protocol PickerViewProtocol {
func onSearchDateChange (date: NSDate)
}
class PickerViewController: UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!
var initDate : NSDate?
var delegate : PickerViewProtocol?
override func viewDidLoad() {
//print("PickerViewController viewDidLoad")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
datePicker.timeZone = NSTimeZone(name: "Europe/Rome")
if initDate != nil {
datePicker.date = initDate!
}
datePicker.addTarget(self, action: #selector(PickerViewController.datePickerChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
}
func setDate(date:NSDate) {
if datePicker != nil {
datePicker.date = date
} else {
initDate = date
}
}
func datePickerChanged(datePicker:UIDatePicker) {
delegate?.onSearchDateChange(datePicker.date)
}
}