How to change the size of row in UIPickerView by button click?

时光怂恿深爱的人放手 提交于 2019-12-11 06:57:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!