Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vanuccif opened this issue Feb 29, 2016 · 5 comments
Closed

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

vanuccif opened this issue Feb 29, 2016 · 5 comments
Labels

Comments

@vanuccif
Copy link

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
Copy link
Owner

@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
Copy link
Owner

@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.

@filipealva filipealva added the bug label Apr 25, 2016
@azhararmar
Copy link

azhararmar commented May 28, 2017

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 filipealva changed the title The defult selected row is not the middle one. [BUG] The defult selected row is not the middle one. Oct 23, 2018
@filipealva filipealva changed the title [BUG] The defult selected row is not the middle one. [BUG] The default selected row is not the middle one. Oct 24, 2018
@filipealva
Copy link
Owner

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
Copy link
Owner

filipealva commented Oct 24, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants