选择器视图swift出错

时间:2016-10-11 23:10:38

标签: ios swift swift3 uipickerview

我试图更改选择器视图的文本字体和颜色。但是,当我这样做时,我得到一个错误,请帮助。

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let attributedString = NSAttributedString(string: "some string", attributes: [NSForegroundColorAttributeName : UIColor.red])
    return attributedString
}

ErrorImage

2 个答案:

答案 0 :(得分:0)

我通过提供这样的视图做了类似的事情

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
          let pickerLabel = UILabel()
          pickerLabel.textColor = UIColor.black
          pickerLabel.text = "\(dataSource[row])"

          pickerLabel.font = UIFont(name: "OpenSans-Light", size: 70)
          pickerLabel.textAlignment = NSTextAlignment.center
          return pickerLabel
       }

func numberOfComponents(in pickerView: UIPickerView) -> Int {
      return 1
   }

   func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
      return dataSource.count
   }

答案 1 :(得分:0)

您必须实现UIPickerViewDataSource希望您实施的方法。其中包括:

func numberOfComponents(in pickerView: UIPickerView) -> Int

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

例如:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return 10
}