在UITableView中selectFirst Row作为默认值

我有一个应用程序是基于viewbased,我添加一个tableview作为子视图到主视图。 我已经采取了UITableViewDelegate响应表方法。 一切工作正常,但我想select第一行或UITableView作为默认选中(突出显示)。

请帮我,我需要什么代码,我需要把它放在哪里。

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; [myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom]; } 

在代码中使用这个最好的方法,如果你想默认select任何一行,在viewDidAppear中使用。

Swit 3.0更新的解决scheme

 let indexPath = IndexPath(row: 0, section: 0) tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom) 
 - (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]; } 
 - (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]; } } 

以下是在Swift 1.2中如何做到这一点:

 override func viewWillAppear(animated: Bool) { let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top) } 

为了只在第一次加载表时select第一个单元格,人们会认为使用viewDidLoad是正确的地方, 但是在执行的时候,表没有加载它的内容,所以它不会工作(并可能崩溃的应用程序,因为NSIndexPath将指向一个不存在的单元格)。

解决方法是使用一个variables,指出表已经加载之前,并相应地做的工作。

 @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 

我们根据它是否是第一个单元格…中间单元格或最后一个单元格,为单元格使用自定义背景图像。 这样我们就可以在整个桌子上看到一个不错的圆angular。 当这个行被选中时,它会交换一个漂亮的“突出显示”单元格,以使用户反馈他们已经select了一个单元格。

 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; 

如果您希望只使第一个单元格(位于indexPath.row == 0)使用自定义背景。

这是来自马特·加拉格尔的优秀网站

这是我对swift 3.0的解决scheme:

 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 } 

你可以这样做:

 - (void)viewDidLoad { [super viewDidLoad]; NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0]; [myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom]; }