Select Default First Row in UITableView

I have an application that is viewbased, and I am adding a tableview view as a view to the main view. I adopted a UITableViewDelegate to respond to table methods. Everything works fine, but I want to select the first row or default UITableView (highlighted).

Please help me with which code I need and where I need to put it.

+52
ios uikit uitableview
Apr 28 '10 at 9:21
source share
9 answers
 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; [myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom]; } 

The best way to use this in your code, if you want to select any row by default, use in viewDidAppear.

+105
May 31 '11 at 7:22 a.m.
source share

Updated Switcher 3.0 Solution

 let indexPath = IndexPath(row: 0, section: 0) tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom) 
+9
Nov 14 '16 at 12:38
source share
 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // assuming you had the table view wired to IBOutlet myTableView // and that you wanted to select the first item in the first section [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0]; } 
+8
Jan 05 2018-12-12T00:
source share
 - (void)viewDidLoad { [super viewDidLoad]; self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController]; if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){ NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop]; [self tableView:self.tableView didSelectRowAtIndexPath:indexPath]; } } 
+5
03 Mar '14 at 5:07
source share

Here's how to do it in Swift 1.2:

 override func viewWillAppear(animated: Bool) { let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top) } 
+3
Aug 14 '15 at 4:26
source share

To select the first cell when loading the table for the first time, you might think that using viewDidLoad is the right place to go, but at that time the table hasnโ€™t loaded its contents, so it wonโ€™t work (and, possibly, the application crashes , since NSIndexPath will point to a nonexistent cell).

The workaround is to use a variable that indicates that the table was loaded before and was doing the appropriate work.

 @implementation MyClass { BOOL _tableHasBeenShownAtLeastOnce; } - (void)viewDidLoad { [super viewDidLoad]; _tableHasBeenShownAtLeastOnce = NO; // Only on first run } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if ( ! _tableHasBeenShownAtLeastOnce ) { _tableHasBeenShownAtLeastOnce = YES; BOOL animationEnabledForInitialFirstRowSelect = YES; // Whether to animate the selection of the first row or not... in viewDidAppear:, it should be YES (to "smooth" it). If you use this same technique in viewWillAppear: then "YES" has no point, since the view hasn't appeared yet. NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection: 0]; [self.tableView selectRowAtIndexPath:indexPathForFirstRow animated:animationEnabledForInitialFirstRowSelect scrollPosition:UITableViewScrollPositionTop]; } } /* More Objective-C... */ @end 
+1
05 Oct '15 at 19:40
source share

Here is my solution for quick 3.0:

 var selectedDefaultIndexPath = false override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if dataSource.isEmpty == false, selectedDefaultIndexPath == false { let indexPath = IndexPath(row: 0, section: 0) // if have not this, cell.backgroundView will nil. tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) // trigger delegate to do something. _ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath) selectedDefaultIndexPath = true } } func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath) cell?.selectedBackgroundView?.backgroundColor = UIColor(hexString: "#F0F0F0") return indexPath } 
+1
May 29 '17 at 12:26
source share

We use custom background images for the cell based on whether it is the first cell ... the middle cell or the last cell. Thus, we get a beautiful rounded corner of the look at the whole table. When a row is selected, it collapses the pretty cell "highlighted" to return to the user channel that they selected the cell.

 UIImage *rowBackground; UIImage *selectionBackground; NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]]; NSInteger row = [indexPath row]; if (row == 0 && row == sectionRows - 1) { rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"]; selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"]; } else if (row == 0) { rowBackground = [UIImage imageNamed:@"topRow.png"]; selectionBackground = [UIImage imageNamed:@"topRowSelected.png"]; } else if (row == sectionRows - 1) { rowBackground = [UIImage imageNamed:@"bottomRow.png"]; selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"]; } else { rowBackground = [UIImage imageNamed:@"middleRow.png"]; selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"]; } ((UIImageView *)cell.backgroundView).image = rowBackground; ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 

If you just want to make the first cell, that is, in indexPath.row == 0, use a custom background.

This comes from Matt Gallagher's excellent site.

0
Apr 29 '10 at 16:29
source share

You can do the following:

 - (void)viewDidLoad { [super viewDidLoad]; NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0]; [myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom]; } 
-one
Jul 20 2018-10-10T00:
source share



All Articles