解雇第二个ViewController后,UITableViewController返回第一个单元格

时间:2016-01-22 10:05:57

标签: ios uitableview swift2

我在UITableViewController中实现了didSelectRowAtIndexPath:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    // determine selected cell
    let name = self.names[indexPath.row]

    // present menu VC
    let destVC: UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MenuItemsReadOnly") as UIViewController

    self.navigationController?.pushViewController(destVC, animated: true)
}

到目前为止一切顺利。它工作正常。解雇destVC后我的问题就开始了:

func doneSelf() {
    self.navigationController?.popViewControllerAnimated(true)
}

解雇后,我回到了我的UITableViewController,但视图一直向上滚动到顶部,以便第一个单元格可见。

我还没有实现viewDidAppear函数。我没有在viewDidLoad中调用tableView.reloadData(),这是我在UITableViewController中实现的唯一与视图相关的方法:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self

    // format navigation controller
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    self.navigationController!.navigationBar.translucent = false
    self.navigationController!.navigationBar.barStyle = .Black
    self.navigationController!.navigationBar.barTintColor = UIColor.flatRedColor()
    self.navigationController!.navigationBar.tintColor = UIColor.flatRedColor()

    self.automaticallyAdjustsScrollViewInsets = false

    self.tableView.backgroundColor = UIColor.flatWhiteColor()

    // format table UI
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None

    // last cell would get cropped off without this
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0)

    // navigation buttons
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "IconMenuShortcut"), style: .Plain, target: self, action: "presentLeftMenuViewController")
    navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()        
}

这是设计的吗?一旦我从第二个UIViewController返回并且不自动移回到顶部,我如何将所选单元格保持在视图中?

1 个答案:

答案 0 :(得分:0)

对所选的indexPath进行参考,并在回归TableviewController时,致电

    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];