【问题标题】:pickerView different row heightpickerView 不同的行高
【发布时间】:2015-10-17 13:46:58
【问题描述】:

我有一个从网站加载数据的 pickerView。我想为pickerView的每一行设置高度。

这是我尝试过的,但似乎 pickerView 只接受所有行的一个值。

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    var height: CGFloat = 40
    if categories.count != 0 {
        return heightForView((categories[component].valueForKey("name") as? String)!, font: UIFont(name: "HelveticaNeue-Thin", size: pickerFontSize!)!, width: pickerView.frame.width) + 10
    }
    return height + 10
}

func heightForView(text: String, font: UIFont, width: CGFloat) -> CGFloat {
    let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.font = font
    label.textAlignment = .Center
    label.text = text
    label.sizeToFit()
    return label.frame.height
}

类别数组第一次为空,rowHeightForComponent 使用常量值。但是当我从 Core Data 加载数据时,我希望我的 pickerView 用新的 componentHeight 重新加载自己,但是当我写的时候它没有被调用:

pickerView.reloadAllComponents()

【问题讨论】:

  • 您确定将 UIPickerView 的委托设置为当前班级吗?
  • @halileohalilei class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {override func viewDidLoad() { super.viewDidLoad() // pickerView delegate n dataSource pickerView.dataSource = self pickerView.delegate = self 对吗?
  • 你是否也实现了方法numberOfComponents:
  • @halileohalilei 是的,我已经实现了 numberOfComponentsInPickerView、numberOfRowsInComponent、titleForRow、viewForRow 和 rowHeightForComponent 方法。

标签: ios swift uipickerview reload


【解决方案1】:

我发现当你调用 reloadAllComponents: 时,所有的委托/数据源方法都会被调用 except rowHeightForComponent:

我找到了可以解决您的问题的解决方法。每次您希望通过调用reloadAllComponents: 来设置行的高度时,重置您的UIPickerView 的委托。像这样的:

pickerView.delegate = self
pickerView.reloadAllComponents()

【讨论】:

    【解决方案2】:

    我猜选择器视图第一次缓存它的大小信息。 因此使用 reloadAllComponents,您可以从数据源和委托更新组件的数据,但不能更新行高。


    解决方法:

    在准备好数据之前,数据源和委托都设置为 nil。 喜欢:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        //Setup UI...
    
        self.pickerView.dataSource = nil
        self.pickerView.delegate = nil
    }
    

    在数据准备好后,正确设置数据源和委托。 喜欢:

    func updateUI() {
    
        // ...data is ready
    
        self.pickerView.dataSource = self // set data source
        self.pickerView.delegate = self // set delegate
        self.pickerView.reloadAllComponents()
    
    }
    

    【讨论】:

    • 如果我第一次没有数据,几秒钟后加载,我该怎么办?
    【解决方案3】:

    heightForView 用于指定高度,viewForRow 允许您自定义行。因此,在heightForView 方法中创建新高度,并在viewForRow 方法中自定义行,而不是像现在那样使用heightForView

    如果没有在heightForView 方法中指定,XCode 无法更改高度。因为它需要heightForView

    最后,当您获得来自网络的数据时,重新加载您的数据。

    【讨论】:

    • in rowHeightForComponent 我只是计算标签的高度然后返回它,我没有在那里进行任何自定义
    • 如果你愿意,我可以粘贴整个代码以便更好地理解
    • 不,我很困惑。也许这个链接会对你有所帮助。 stackoverflow.com/questions/6397947/…
    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多