How to set different heights for rows of same UIPickerView?

Hello,


I have an UIPickerView that works fine and loads its data from separate arrays. Using XCode 7.1 and Objective-C for iOS development targeted for iOS 9.3.


One of the rows is of just plain string text. The other row of png images. The images are 200 by 200 pixes each. The preferred row height I have then is:


- (CGFloat) pickerView: (UIPickerView *) pickerView rowHeightForComponent:(NSInteger)component

{

if(component == wordPicker){ //checks for component of words to set that as appropriate row height

return 60.0;

}

else{ //checks for component of images imagePicket to set 200 as appropriate row height.

return 200.0;

}


However, even though this logic is similarly and successfully implemented for "widthForComponent," the rowHeightForComponent has the effect of setting up the heighest row value as the row height for both picker row components. As a result the image picker row is displayed as expected, yet the word picker next to it has same 200 pixels height, and words' cells are large enough that the words above and below can't be seen unless scrolled through...


I have experimented with several suggestions of using reloadAllComponents method, setting up a for loop to force independent row heights. None has worked. And searched through the internet and this forum and couldn't find a definitive answer.


Am I trying to do something iOS doesn't support with its delegate methods and current framework for UIPickerView? Or am I missing a key method?


At this point I am inclined to attempt multiple Pickers in the same class.

It's unclear what you mean by "One of the rows is of just plain string text. The other row of png images."


A picker has different columns and each column has many rows. The column is called a component and is numbered from 0 to a few (like 2 or 5). There can be many rows. In your code, if wordPicker was 0 or 1 it would make sense - that would be the column, or component, that had the words in it. Each row in that column would have a height of 60. And each row in the other columns would have a height of 200.


I suspect that wordPicker is not an NSInteger less than the number of columns (aka components) in the picker. The number of columns is set by your response to:


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 2;

}

Hello PBK, Correct. My apologies in not clarifying it. One column is of imageComponent = 0, and the other stringComponent = 1. And I attempted both ways, of return for number of components: Return 2; //later Return component.count; In so much that when setting total component count with a mistake, only one row is displayed in Picker. In my situation, both are displayed correctly and return values for which each individually selected. I am even able to set the width for each row. Though I am not able to set individual row height.

You wrote, again "Though I am not able to set individual row height."


You are attempting to set the row height for component 0 to 200 and to component 1 to 60.

You need to return "60" if "component==0" not "==wordPicker"

Correct.

Again my apologies I didn't paste my code earlier because I was away from my computer (phone posting...). The following is what I had originally. Currently attempting to create multiple pickers with tags in same View Controller:


- (CGFloat) pickerView: (UIPickerView *) pickerView rowHeightForComponent:(NSInteger)component

{

if(component == stringComponent){

return 60.0;

}

else{ //imageComponent

return 200.0;

}

}


The code above will set always the highest value as the height for all rows. So as an experiment I interchanged the values of the IF and ELSE to make sure they were valid. So I attempted values such as (60, 200), (60, 60), (60, 10), (10, 10), (200, 60), (10, 60). Always the highest value is set as the height for both rows. Meaning that indeed the components are individually recognized, still the value passed for row height is not individually set.

Before you conclude that you can't have a different height for all the rows in one component versus another component you need to verufy that you are, in fact, returning '60' or '200' - how do you know that stringComponent==0 and not 43?


Add an

NSLog(@"returning 60 with component = %i",component);

before the

return 60:

and be sure it fires.

I tried it and confirm that the row height for all rows in a picker is the largest value returned by the method rowHeoghtForComponent for any row in the picker. I don't think you can do what you want to do.

Thank you for helping and confirming PBK!


My macbook's hard drive SATA cord failed and wasn't recognizing any of hard disks larger than 200 gigs... ordered a new one and finally got back in working.


Unfortunately I have same result and it is as I suspected...


I will resume my attempts in creating multiple rows and use the same method for setting the row heights individually through the tag separated rows.

How to set different heights for rows of same UIPickerView?
 
 
Q