[英]How to create a view with two tableviews, each reusing a pickerview on every row
[英]two labels for each row with pickerView
我正在嘗試實現viewForRow
委托,所以我可以為每一行有兩個標簽。 我目前每行只處理一個標簽。 任何幫助都受到高度贊賞。
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
var returnLabel = view as UIView!
if view == nil {
returnLabel = UIView()
}
var pickerLabel = view as! UILabel!
if view == nil {
pickerLabel = UILabel()
}
let titleData = List
let myTitle = NSAttributedString(string: titleData[row], attributes: [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 17.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
pickerLabel.attributedText = myTitle
pickerLabel.textAlignment = .Center
var pickerLabel2 = view as! UILabel!
if view == nil {
pickerLabel2 = UILabel()
}
let subtitleData = subtitleList
let mySubtitleTitle = NSAttributedString(string: subtitleData[row], attributes: [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 12.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
pickerLabel2.attributedText = mySubtitleTitle
pickerLabel2.textAlignment = .Left
returnLabel.addSubview(pickerLabel)
returnLabel.addSubview(pickerLabel2)
return returnLabel
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 60
}
您沒有為標簽指定框架。 參考完整片段:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let rowHeight:CGFloat = 60.0
let List = ["data1_1", "data1_2", "data1_3"]
let subtitleList = ["data2_1", "data2_2", "data2_3"]
override func viewDidLoad() {
super.viewDidLoad()
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
var returnLabel: UIView!
var pickerLabel: UILabel!
var pickerLabel2: UILabel!
if view == nil {
returnLabel = UIView(frame: CGRectMake(0, 0, pickerView.frame.size.width, rowHeight))
pickerLabel = UILabel(frame: CGRectMake(0, 0, returnLabel.frame.size.width, rowHeight / 2))
pickerLabel2 = UILabel(frame: CGRectMake(0, rowHeight / 2, returnLabel.frame.size.width, rowHeight / 2))
returnLabel.addSubview(pickerLabel)
returnLabel.addSubview(pickerLabel2)
}
// title
let titleData = List
let myTitle = NSAttributedString(string: titleData[row], attributes: [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 17.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
pickerLabel.attributedText = myTitle
pickerLabel.textAlignment = .Center
// subtitle
let subtitleData = subtitleList
let mySubtitleTitle = NSAttributedString(string: subtitleData[row], attributes: [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 12.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
pickerLabel2.attributedText = mySubtitleTitle
return returnLabel
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return rowHeight
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return List.count
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.