Search code examples
swiftswift3

How to get row number in pickerview rowHeightForComponent?


I have a pickerview that may have different height of the row depends on the text on passed array:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44));
    label.lineBreakMode = .byWordWrapping;
    label.numberOfLines = 0;
    label.text = array[row]
    label.sizeToFit()

    return label;
}

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
    // How to get the the row number of pickerview here?
    return 0
}

Solution

  • I have a pickerview that may have different height of the row depends on the text on passed array

    No, you don't have such a picker view. Picker views can't have variable row heights. The reason you can't find a way to do it is that there's no such way.