filipealva / PickerView

🔸 A customizable alternative to UIPickerView in Swift.
MIT License
527 stars 92 forks source link

[BUG] The default selected row is not the middle one. #8

Closed vanuccif closed 5 years ago

vanuccif commented 8 years ago

Hi, thank you so much for this, it's really helpful! I noticed that the default selected row is not the middle one. I think the problem is in this code:

Int(ceil(Float(numberOfRowsByDataSource) / 2.0))

Since the row is an index, you have to subtract 1 to numberOfRowsByDataSource, to get the middle one. For example if we have 5 items, ceil(2.5) is 3, but the middle item index is actually 2.

It would be great to have a public function (or delegate function) that lets you set the first selected item.

Thanks

filipealva commented 8 years ago

@vanuccif Hello

Thank you for caring about it. I'm so happy that you found it useful.

Sorry about the delay, but I think you can set the currentSelectedRow property before presenting it and you'll have the behavior you need.

Hope it solve this issue.

All the best

filipealva commented 8 years ago

@vanuccif Hello!

Just to add: It is a bug. I will fix it, thanks for reporting.

Until a version with this fix is not released you can use the currentSelectedRow to set the first selected item.

azhararmar commented 7 years ago

I faced the same issue and using currentSelectedRow did solve the issue temporarily, the problem with currentSelectedRow is it does not work in some cases where you have have say 100 rows, In my case it was from year 1900 to 2017 for year picker. If I give the value as 50 so that It centers at 1950, the highlight goes in the bottom element, bit if I give the currentSelectedRow as 2, it centers in the middle with 1902 selected. Thus disallowing me to center the element at 1950 make it center and highlight it.

It would be nice to have this fix.

One more suggestion is to add currentSelectedRow in the documentation, I could only find it after going through issues.

filipealva commented 5 years ago

Hi @azhararmar,

I'm getting back to this project, organizing the things and fixing any bug that I wind up finding.

I simulated the exact same scenario you provided on your comment and it worked properly setting the currentSelectedRow to 50, as we can see below:

However, I fixed an issue some time ago that was causing the items to be selected at the bottom of the picker, maybe it was this other issue that was affecting you.

Please, if you still suffer with this issue, make sure to update the PickerView to the last version available (0.3.4 today).

filipealva commented 5 years ago

For example if we have 5 items, ceil(2.5) is 3, but the middle item index is actually 2.

@vanuccif Thinking again on this issue, actually the middle index for this case is subjective. If you want to round down, then the middle index is 2, but if you want to round up the middle index is 3. It is a matter of opinion.

If we use floor instead of ceil it will round down, and we'll have the result you want. I can make this change, but it's not a bug.

EDITED: Sorry for the confusion, just to clarify: ceil rounding 2.5 to 3 is not a bug, but not subtracting 1 to calculate indexes is a bug indeed :P