5

In NativeiOS you can make a UITableView scroll to a particular UITableViewCell using:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:87 inSection:0] 
         atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

.. or similar.

For example, if the ListView held 100 items and I wanted to scroll such that the 87th item is visible.

Looking at the React Native ListView Docs there's nothing obvious there.

Has anyone managed to achieve this? I realise that ListView does not wrap UITableView so we can't just expose the method.

EDIT: Looks like some support for this is in the 0.19-rc release, check this https://github.com/facebook/react-native/releases

https://github.com/facebook/react-native/commit/33e05a11f00d1455f39b6dfef1c76c4130df02e5

Ben Clayton
  • 80,996
  • 26
  • 120
  • 129
  • This looks relevent: http://stackoverflow.com/questions/33391040/how-to-start-listview-scrolled-to-a-certain-position-in-react-native?rq=1 – Ben Clayton Jan 13 '16 at 07:51
  • 1
    Unfortunately the support you're referencing to in the `0.19.0-rc` is just an alias to manually accessing the scroll responder to scroll to a specific **coordinate**. I'm having the same problem and doing the same investigation. Currently one has to calculate the offset and manually scroll to an offset. – Atticus Jan 30 '16 at 21:22

1 Answers1

4

Use a ScrollView with contentOffset={{ x: offsetX, y: offsetY }} and calculate the offset variables depending on which row you want to scroll to.

jamesfzhang
  • 4,403
  • 8
  • 32
  • 43
  • Do you know if you can set contentOffset on the ScrollView (which I assume) lives under the ListView? – Ben Clayton Jan 12 '16 at 18:56
  • 2
    @BenClayton I believe you can still use a `ListView` and just set `renderScrollableComponent`or `renderScrollComponent` (depending on which version of RN you're using) to return a `ScrollView` with `contentOffset` (haven't tested this specific case yet). – jamesfzhang Jan 12 '16 at 19:09
  • 2
    Right now It can't be done if height or width rows is variable. I feel its a big lost feature in React Native. I hope soon they realize its so important to scroll to a certain rowID. In my project a made an workaround, but I don't like it, so... – Philip Enc Mar 31 '17 at 15:55