冰雹
我很难编写干净的代码。因此,我想在单独的类文件和扩展中保留我的数据源和deletegate。所以想要更接近M-V-C风格。因此,我的模型与我的所有控制器类和视图类分开。但填充pickerView对象时出现问题。这是代码;
//我的代表和数据源 进口基金会 导入UIKit
class MyPickerView: NSObject{
var data = PickerViewData.getData()
}
extension MyPickerView: UIPickerViewDataSource{
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 1
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return self.data.count
}
}
extension MyPickerView: UIPickerViewDelegate{
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent
component: Int) -> CGFloat {
return 100
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int,
forComponent component: Int, reusing view: UIView?) -> UIView {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let dayLbl = UILabel(frame: CGRect(x: 0, y: 10, width: 100, height: 15))
let priceLbl = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let petLbl = UILabel(frame: CGRect(x: 0, y: 78, width: 100, height: 15))
dayLbl.text = data[row].dayName
priceLbl.text = String(data[row].price)
petLbl.text = data[row].petName
dayLbl.textAlignment = .center
priceLbl.textAlignment = .center
petLbl.textAlignment = .center
view.addSubview(dayLbl)
view.addSubview(priceLbl)
view.addSubview(petLbl)
return view
}
}
//我的模特
import Foundation
struct PickerViewModel{
var dayName:String!
var price:Double!
var petName:String!
init(dayName:String,price:Double,petName:String) {
self.dayName = dayName
self.price = price
self.petName = petName
}
}
struct PickerViewData {
static func getData() -> [PickerViewModel]{
let m = PickerViewModel(dayName: "Pazartesi", price: 32.3,
petName: "Köpek")
let p = PickerViewModel(dayName: "Salı", price: 32.3, petName:
"Kuş")
let z = PickerViewModel(dayName: "Çarşamba", price: 32.3,
petName: "Kedi")
let t = PickerViewModel(dayName: "Perşembe", price: 32.3,
petName: "Domuz")
let k = PickerViewModel(dayName: "Cuma", price: 32.3, petName:
"Kanarya")
return [m,p,z,t,k]
}
}
//我的控制器
import UIKit
class FirstVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
let pickerView = UIPickerView(frame: CGRect(x: 100, y: 100,
width: 200, height: 500))
let deldat = MyPickerView()
pickerView.delegate = deldat
pickerView.dataSource = deldat
self.view.addSubview(pickerView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:3)
选择器视图的dataSource
和delegate
属性被声明为weak
- 这意味着他们不会保留您的MyPickerView
实例。由于您的实例是局部变量,因此只要viewDidLoad
退出,它就会被释放。
您需要将MyPickerView
实例保存在实例属性中,以便在视图控制器的生命周期内保留它:
import UIKit
class FirstVC: UIViewController {
private var deldat: MyPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
let pickerView = UIPickerView(frame: CGRect(x: 100, y: 100,
width: 200, height: 500))
self.deldat = MyPickerView()
pickerView.delegate = deldat
pickerView.dataSource = deldat
self.view.addSubview(pickerView)
}
}