我尝试使用以下方法自定义pickerview行视图:
pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView
我遇到的问题是我从nib / xib加载的视图无法获得正确的行大小。
这是我的代码:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
if let reuseView = view as? ImageTypePickerView {
return reuseView
} else {
let myRowView = ImageTypePickerReusableView()
let indexPath = IndexPath(row: row, section: component)
if let myUnitImageType = self.frc?.object(at: indexPath) as? UnitImageType {
let image = UIImage(data: myUnitImageType.imageData! as Data)
myRowView.typeImage.image = image
myRowView.typeLabel.text = myUnitImageType.name
myRowView.frame = CGRect(x: 0, y: 0, width:100, height: 108)
}
return myRowView
}
}
我应该也用吗? :
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 108
}
? ? ?