UITableview以编程方式选择起始单元格

时间:2010-12-10 10:28:29

标签: iphone

我在iPhone应用程序中使用了UItableview。我使用Tableviewcell单击事件移动到下一个视图。但是当我点击uinavigation的后退按钮时,tableview单元格选择就像之前的点击一样。

但是,我需要,当我点击导航的后退按钮时,光标位置移动到tableview的起始单元格。

也就是当我单击后退按钮时,表格视图会自动选择第一个单元格。

3 个答案:

答案 0 :(得分:5)

试试这个::

- (void)viewWillAppear:(BOOL)animated
{   
    NSIndexPath *ip = [NSIndexPath indexPathForRow: 0 inSection:0];
    [self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionTop];
}

答案 1 :(得分:1)

我想现在你的导航栏按钮只是弹出视图,你需要推动视图,以便你的应用程序将重新加载包含表的类的视图。

在viewDidLoad中写下这行代码:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title Of Button" style:UIBarButtonItemStyleBordered target:self action:@selector(functionToPushView:)];

Now define the function like this:

-(void)functionToPushView: (id)sender
{
     YourClass *objYourClass = [[YourClass alloc] init];
     [[self navigationController] pushViewController:objYourClass animated:YES];
}

请记住,“YourClass”将是包含UITableView的类。

答案 2 :(得分:0)

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedItem = indexPath
    let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "web") as! WebViewController
    webVC.url = self.news?[indexPath.item].url
    webVC.selectItemReturn = selectedItem
    self.present(webVC, animated: true, completion: nil)

}
override func viewWillAppear(_ animated: Bool) {
    self.newsTable.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
    self.newsTable.selectRow(at:selectedItem, animated: false, scrollPosition:.top)
}

单击后退按钮时,UITableview会自动选择单元格