在UITableView中选择First Row作为默认值

tus*_*yar 54 uitableview uikit ios

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

请帮助我,我需要什么代码以及我需要放在哪里.

Zai*_*aza 110

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

在代码中使用它的最佳方法是,如果要在默认情况下选择任何行,请在viewDidAppear中使用.

  • 由于一些奇怪的原因,我的单元格不会在viewDidAppear中选择,但它在viewWillAppear中工作正常 (4认同)

Sou*_*rma 11

Swit 3.0更新了解决方案

let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
Run Code Online (Sandbox Code Playgroud)


Ank*_*yas 8

- (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];
    }
Run Code Online (Sandbox Code Playgroud)


mal*_*hal 5

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)


San*_*Ray 5

斯威夫特 4 更新:

func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let indexPath = IndexPath(row: 0, section: 0)
    myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
}
Run Code Online (Sandbox Code Playgroud)

如果要选择不同部分中的任何其他行,请更改行和部分值。


Ash*_*shu 5

斯威夫特 5.x 更新:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let indexPath = IndexPath(row: 0, section: 0)
    tableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
    tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath)
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*son 1

我们根据单元格是否是第一个单元格...中间单元格或最后一个单元格使用自定义背景图像。这样我们的整个桌子就会有一个漂亮的圆角外观。当选择该行时,它会交换一个漂亮的“突出显示”单元格,以向用户反馈他们已选择了一个单元格。

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;
Run Code Online (Sandbox Code Playgroud)

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

这源自 Matt Gallagher 的优秀网站