实际上,我想在UIPickerView
中仅显示两行数据超过2行。因此,每当用户向上/向下滚动时,用户只能显示两行。
通过以下图片了解我想要的内容:
我试过下面的代码
var pickerDataSource = ["1", "2", "3", "4","5","6", "7", "8", "9","10","11","12"];
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView
{
var label = view as! UILabel!
if label == nil {
label = UILabel()
}
let titleData : String?
titleData = pickerDataSource[row]
if row == pickerView.selectedRowInComponent(0)
{
let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName: UIFont(name:"Montserrat-Regular", size: 16.0)!,NSForegroundColorAttributeName:UIColor.lightGrayColor()])
label.attributedText = myTitle
}
else
{
let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName: UIFont(name:"Montserrat-Regular", size: 16.0)!,NSForegroundColorAttributeName:UIColor.redColor()])
label.attributedText = myTitle
}
label.textAlignment = .Center
return label
}
这将改变行的颜色,但我只设置两行。
有没有办法实现这样的目标?