5

I am saving the indexPath selection from a subview and passing it to the parent view with delegates. When I got back to my subview from my main view I pass the indexPath back to it and show the user which cell they previously selected with a tick in the accessory view of the tableviewcell.

One problem being if the user has selected a cell out of a fairly big list its hard to find the cell they selected again incase they wanted to change it (being that they made a mistake)

I would like to know if their is a way to use indexPath or something similar to center the previously selected cell of the uitableview to the center of the screen?

UPDATE::

Here is a graphical view of what I am trying to achive to make it abit more understandable..

step one : select cell then go to subview and select the cell (value) you want to pass back to main view (save indexPath of selected cell) enter image description here

step two: user either wants to change his selection or made a mistake and was ment to select the cell below the one they chose... repeat previous steps but display previously selected cell in the center of the view..

enter image description here

C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • Is this a problem with `cell reusability` ? What do you mean by `to center the previously selected cell of the uitableview to the center of the screen` ??? – Legolas Oct 06 '11 at 20:55
  • no problem with reusability, What I mean by centering the cell to the center of the screen I mean when the user selects a cell from the subview they are instantly directed back to the main view... if they then realize that they have chosen the wrong cell I would like them to be able to go back to the subview and change their selection... however if they have chosen something way way down in the tableview if makes it hard for them to find their selection again... so I would like to center this (the previously selected cell) on the screen as soon as the view loads for the user – C.Johns Oct 06 '11 at 21:02
  • So yeah, ScrollToIndexPath should work ;) Good job with the images. – Legolas Oct 06 '11 at 22:06
  • thanks :) still working on it as im getting some weird errors.. – C.Johns Oct 07 '11 at 00:23

2 Answers2

12

Have you tried the following function

[tableView scrollToRowAtIndexPath: atScrollPosition:UITableViewScrollPositionMiddle animated:YES]
BumbleBoks
  • 227
  • 2
  • 5
  • wow.. this is exactly what I want.. I had a read about it and shoul definatly work.. however I am getting an error when I try to use it as the first time my table loads I dont have "oldCheckedData" set.. [self.tableView scrollToRowAtIndexPath:oldCheckedData atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; – C.Johns Oct 06 '11 at 22:02
  • I'm glad that it's useful. If the oldCheckedData is not set, maybe you can initialize it using [NSIndexPath indexPathForRow:0 inSection:0] to scroll to the top of the table the first time. – BumbleBoks Oct 06 '11 at 22:11
  • I have been doing this in my viewDidLoad method if (oldCheckedData != NULL) { [self.tableView scrollToRowAtIndexPath:oldCheckedData atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } However I keep getting this error ***** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678** – C.Johns Oct 06 '11 at 22:20
  • I would say use it in viewDidAppear:(BOOL)animated method. That would make sure that the function is called after the view is added to the window. Or you can use it in viewWillAppear: method ... – BumbleBoks Oct 06 '11 at 22:23
  • Try using [oldCheckedData length] in the if condition instead of oldCheckedData != NULL – BumbleBoks Oct 06 '11 at 23:06
  • sweet it works perfectly.. I had done something else while trying to get this to work and not gone back and fixed it.. however I am not really sure where to use this peice of code.. currenlty I am using it in cellForRowAtIndexPath and its screwing up my scrolling... – C.Johns Oct 07 '11 at 00:42
0

a UITableView is a subclass of UIScrollView - try setting the Content Offset (figure out how much with the cell height and indexPath).

[tableView setContentOffset:CGPointMake(0, indexPath.row*cellHeight) animated:YES];

should work. You might want to do the math a little differently.

Evan Davis
  • 462
  • 1
  • 4
  • 9
  • would I call this in viewdidload or in another method? – C.Johns Oct 06 '11 at 20:54
  • I'm not sure if I completely understand the design of the app, but from what I gather you want to scroll the table to the previously selected when you select a new cell, and you send the indexPath to the parent view with a delegate. I would image you want to do it when that happens, so set it up in your delegate method that's called when you select a new cell. – Evan Davis Oct 06 '11 at 20:59