Swift 中的 UIPickerView 子类:viewForRow 中的错误可选值
UIPickerView subclass in Swift : error optional value in viewForRow
我想继承 UIPickerView 以创建自定义 DatePicker。我试过了,但是 Swift 中有一个错误:
unexpectedly found nil while unwrapping an Optional value
这一行:(view.viewWithTag(1) as UILabel).text = array[row]
这是代码:
class MyPickerView : UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate{
let array = ["one", "two", "three"]
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame:frame)
self.delegate = self
self.dataSource = self
}
//components / row
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
//view for row
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
if view == nil {
var view = UIView(frame: CGRectMake(0,0, 150,50))
let label = UILabel(frame:CGRectMake(0,0, 80, 40))
label.tag = 1
view.addSubview(label)
}
(view.viewWithTag(1) as UILabel).text = array[row]
return view
}
//did select row
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println("has selected row \(row)")
}
谢谢
首先你检查视图是否为nil,但是在关闭之后你尝试访问它是否为nil。如果我理解正确你的意图,你应该替换
var view = UIView(frame: CGRectMake(0,0, 150,50))
和
view = UIView(frame: CGRectMake(0,0, 150,50))
因为此时您正在 if 语句的范围内创建一个名为 view 的新变量。
UIPickerView 中有一个已知错误(我认为是从 iOS 7 开始的),即视图永远不会被重用。要解决您的问题,请去掉 if 子句(这样就不需要标签),
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
var view = UIView(frame: CGRectMake(0,0, 150,50))
let label = UILabel(frame:CGRectMake(0,0, 80, 40))
view.addSubview(label)
label.text = array[row]
return view
}
您收到错误的原因是因为您有两个不同的对象 "view"。您在 if 子句内创建的视图在该子句外不可见,因此您调用 viewWithTag 的 "view" 是传递给该方法的视图,并且该视图始终为 nil。
我想继承 UIPickerView 以创建自定义 DatePicker。我试过了,但是 Swift 中有一个错误:
unexpectedly found nil while unwrapping an Optional value
这一行:(view.viewWithTag(1) as UILabel).text = array[row]
这是代码:
class MyPickerView : UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate{
let array = ["one", "two", "three"]
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame:frame)
self.delegate = self
self.dataSource = self
}
//components / row
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
//view for row
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
if view == nil {
var view = UIView(frame: CGRectMake(0,0, 150,50))
let label = UILabel(frame:CGRectMake(0,0, 80, 40))
label.tag = 1
view.addSubview(label)
}
(view.viewWithTag(1) as UILabel).text = array[row]
return view
}
//did select row
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println("has selected row \(row)")
}
谢谢
首先你检查视图是否为nil,但是在关闭之后你尝试访问它是否为nil。如果我理解正确你的意图,你应该替换
var view = UIView(frame: CGRectMake(0,0, 150,50))
和
view = UIView(frame: CGRectMake(0,0, 150,50))
因为此时您正在 if 语句的范围内创建一个名为 view 的新变量。
UIPickerView 中有一个已知错误(我认为是从 iOS 7 开始的),即视图永远不会被重用。要解决您的问题,请去掉 if 子句(这样就不需要标签),
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
var view = UIView(frame: CGRectMake(0,0, 150,50))
let label = UILabel(frame:CGRectMake(0,0, 80, 40))
view.addSubview(label)
label.text = array[row]
return view
}
您收到错误的原因是因为您有两个不同的对象 "view"。您在 if 子句内创建的视图在该子句外不可见,因此您调用 viewWithTag 的 "view" 是传递给该方法的视图,并且该视图始终为 nil。