繁体   English   中英

展开Segue,在添加单元格后滚动到UITableView的底部

[英]Unwind Segue, scroll to bottom of UITableView after adding cell

我有一个UITableViewController that calls another UIViewController`。 他的视图控制器的唯一目的是输入一些数据,然后使用这些数据来创建新的单元格。 插入单元格后,我想滚动到刚添加的单元格。 我所能做的就是滚动到倒数第二个单元格。 这是我的代码。

var stringItems: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"]
override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return stringItems.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel!.text = stringItems[indexPath.row]

    return cell
}

@IBAction func backToTable(segue: UIStoryboardSegue) {
    let vc = segue.sourceViewController as! AddView

    stringItems.append(vc.dataItemOne.text)

    //Insert the row
    let indexPath = NSIndexPath(forRow: tableView.numberOfRowsInSection(0), inSection: 0)
    tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom)

    //Scroll to last item in the list.  Not working.  Scrolls to second to last row.
    let pathToLastIndex = NSIndexPath(forRow: indexPath.row, inSection: 0)
    tableView.scrollToRowAtIndexPath(pathToLastIndex, atScrollPosition: UITableViewScrollPosition.None, animated: false)
}

backToTable函数是UIViewController的展开函数,用于收集要添加到新单元格的数据。

发生的情况是,直到执行backToTable中的代码后才真正添加该单元格。 此代码选择最后一个单元格,但由于电池尚未在代码执行时添加,它最终选择第二到最后一个单元格。

我已经考虑了很多想法,例如在加载另一个视图之前添加一个空白单元格,然后将其填充到backToTable中,但这都不起作用。 通过这种方式,我得到了相同的结果。

关于如何进行这项工作的任何想法? 我唯一能想到的其他想法是设置一个标志以指示添加,然后在viewDidAppear选择正确的单元格。 那可能行得通,但是必须有更好的方法来做到这一点。

我只是尝试了一下。 它有效,但是就像我说的那样,也许不是处理它的最佳方法。

var stringItems: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"]
var addingItem: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
}

override func viewDidAppear(animated: Bool) {
    println(tableView.numberOfRowsInSection(0))

    //If adding, this appears to be the place to scroll to the bottom.
    if addingItem {
        addingItem = false

        let indexPath = NSIndexPath(forRow: tableView.numberOfRowsInSection(0)-1, inSection: 0)
        let pathToLastIndex = NSIndexPath(forRow: indexPath.row, inSection: 0)
        tableView.scrollToRowAtIndexPath(pathToLastIndex, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return stringItems.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel!.text = stringItems[indexPath.row]
    println("cellForRowAtIndexPath")
    return cell
}

@IBAction func backToTable(segue: UIStoryboardSegue) {
    let vc = segue.sourceViewController as! AddView

    stringItems.append(vc.dataItemOne.text)

    //Insert the row
    let indexPath = NSIndexPath(forRow: tableView.numberOfRowsInSection(0), inSection: 0)
    tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    addingItem = true
}

(未经测试),但您的backToTable和viewDidAppear应该类似于以下内容:

@IBAction func backToTable(segue: UIStoryboardSegue) {
    let vc = segue.sourceViewController as! AddView
    stringItems.append(vc.dataItemOne.text)
    addingItem = true
   }

override func viewDidAppear(animated: Bool) {
   if (addingItem){
      tableView.beginUpdates()

      //Insert row

      tableView.endUpdates()
      tableView.reloadData()

      //Scroll to last row
      addingItem = false
   }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM