Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initially select a row in UITableView

I can not find a good spot to call selectRowAtIndexPath:animated:scrollPosition: to initially select a row in UITableView. Table data have not been loaded when the table view controller is initialized, so I can't do selection immediate after the initialization of my UITableViewController(Over bound exception would occur otherwise).

like image 692
an0 Avatar asked Dec 04 '09 09:12

an0


People also ask

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How do I get a cell in Didselectrowat?

Use the below function and pass the index path of the selected row so that the particular cell is reloaded again. Another solution: store the selected row index and do a reload of tableview. Then in cellForRowAtIndexPath check for the selected row and change the accessory view of the cell.


3 Answers

You need to use viewWillAppear: to set the state of a view before it appears, every time it appears, even if the view has already been displayed previously. Any non-visible view in a UITabViewController or UINavigationViewController can be unloaded at any time, so you cannot count on a non-visble view to retain its state.

Or, for finer control, implement loadView, viewDidLoad, and viewDidUnload.

If you are maintaining your own hierarchy of view controllers you will have to manually forward the viewWillAppear, viewWillDisappear, etc., messages to your sub-view controllers.

like image 70
Darren Avatar answered Oct 07 '22 04:10

Darren


Try and overload viewWillAppear:animated or viewDidAppear:animated make sure to call super as well.

Make sure to call selectRowAtIndexPath:indexPath animated:NO as well, it will select without the time delay.

like image 36
Jessedc Avatar answered Oct 07 '22 02:10

Jessedc


you can set any cell selected in cellforrowatindexpath method of uitableview.By using cell.selected=YES;

like image 44
Rahul Vyas Avatar answered Oct 07 '22 02:10

Rahul Vyas