似乎很多人只是返回UILabel,但就我而言,我正在向视图添加标签。视图永远不会为零。这与UIPickerView的用法不同,因为我将其组件旋转了90度,并使其横向滚动而不是上下滚动。
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
if view == nil {
let customWidth = 300
let customHeight = 300
let view = UIView(frame: CGRect(x:0, y:0, width:customWidth, height:customHeight))
let label = UILabel(frame:CGRect(x:0, y:0, width:customWidth, height:customHeight))
if let filter = filters.first(where: {$0.pickerViewRowIndex == row}) {
var leading = ""
if appPurchased == false && filter.requiresPurchase == true { leading = "" }
label.text = "\(leading)\(filter.nameStr)"
}
label.textColor = UIColor.black
label.font = UIFont(name:"HelveticaNeue-Bold", size: 18.0)
label.textAlignment = .center
view.addSubview(label)
view.transform = CGAffineTransform(rotationAngle: 90 * (.pi/180))
label.layer.masksToBounds = false
return view
}else{
print("not nil") // console never prints this line
}
guard let reusedView = view else {
assertionFailure("pickerView label view container never set")
return view!
}
return reusedView
}
答案 0 :(得分:1)
视图永远不会为零。
正确。该文档是错误的。实际上,没有像在选择器视图<xsl:text>\page Table of Contents\line</xsl:text>
<xsl:text>{\field{\*\fldinst {\\TOC \\f \\h}}}</xsl:text>
中重用视图那样的事情。您始终需要创建一个新视图并返回它。