关于ios:uipickerView不同的行高 | 珊瑚贝

pickerView different row height


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 重新加载自己,但是当我写的时候它没有被调用:

1
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 方法。


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

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

1
2
pickerView.delegate = self
pickerView.reloadAllComponents()

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

解决方法:

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

1
2
3
4
5
6
7
8
override func viewDidLoad() {
    super.viewDidLoad()

    //Setup UI…

    self.pickerView.dataSource = nil
    self.pickerView.delegate = nil
}

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

1
2
3
4
5
6
7
8
9
func updateUI() {

    // …data is ready

    self.pickerView.dataSource = self // set data source
    self.pickerView.delegate = self // set delegate
    self.pickerView.reloadAllComponents()

}

  • 那么,如果我第一次没有数据并且它在几秒钟后加载,我该怎么办?


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

并且没有在 heightForView 方法中指定,XCode 不能改变高度。因为它需要 heightForView。

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

  • 在 rowHeightForComponent 我只是计算标签的高度然后返回它,我没有在那里进行任何自定义
  • 如果你愿意,我可以粘贴整个代码以便更好地理解
  • 不,我很困惑。也许这个链接会对你有所帮助。 stackoverflow.com/questions/6397947/…


来源:https://www.codenong.com/31652002/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9230.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?