ios - pickerView 每行有两个标签

标签 ios swift uipickerview

我正在尝试实现 viewForRow 委托(delegate),这样我就可以为每一行设置两个标签。我目前让它每行只使用一个标签。非常感谢任何帮助。

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
    }

}

关于ios - pickerView 每行有两个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733259/

相关文章:

iphone - 显示搜索时,UITableView 被新的 UITableView 隐藏

ios - 我的应用程序因 'Could not cast value of type x to ' UITableViewdelegate' 而崩溃

ios - 如何在初始化后更改 UIPickerView 中的行数

ios - 是否可以同时为 UIPickerView 和 UITableview 设置动画

arrays - 使用元组和 3 个 UIPickerview 计算小计

ios - UITableView 滚动条没有拉伸(stretch)

ios - 如果我们使用dispatch_apply,如何打破循环?

iOS 签名丢失或无效 : The bundle is not signed using an Apple Submission Certificate

ios - 收集在两个精确日期之间拍摄的 iPhone 库照片

inheritance - Swift 语言中的抽象类