问题
I tried to do it in this way: call reloadAllComponents:
when button is clicked but it doesnt call rowHeightForComponent:
回答1:
From the documentation, it looks like reloadAllComponents:
just calls the delegate for new data inside the components, but presumably doesn't ask for the size.
Instead, I would try calling rowSizeForComponent:
on each component. In the documentation for this method, it says, "Returns: The size of rows in the given component. This is generally the size required to display the largest string or view used as a row in the component. A picker view fetches the value of this property by calling the pickerView:widthForComponent: and pickerView:rowHeightForComponent: delegate methods, and caches it. The default value is (0, 0)."
Then, in your UIPickerViewDelegate, I would implement both pickerView:widthForComponent:
and pickerView:rowHeightForComponent:
.
回答2:
You don't need to release pickerView to set new cell height like @auspicious99
proposed.
Instead set the delegate or dataSource each time you trigger reloadAllComponents like this:
pickerView.delegate = self; //or pickerView.dataSource = self;
[pickerView reloadAllComponents];
It's really strange why reloadAllComponents methods doesn't trigger rowHeightForComponent method, but this looks like to be a valid workaround using above trick.
Hope it helps.
回答3:
You might need to release the UIpickerview and create another one with the appropriate return value in pickerView:rowHeightForComponent:
This seems like a pain, but I can't think of another solution offhand.
Unfortunately, it seems that calling rowSizeForComponent won't help, because, as the docs say, "A picker view fetches the value of this property by calling the pickerView:widthForComponent: and pickerView:rowHeightForComponent: delegate methods, and caches it." So, calling rowSizeForComponent only provides the cached values, rather than calling pickerView:rowHeightForComponent: to pick up the new value.
来源:https://stackoverflow.com/questions/10331188/how-to-change-the-size-of-row-in-uipickerview-by-button-click