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

时间:2015-08-22 04:01:02

标签: ios swift uitableview

我有一个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
}

1 个答案:

答案 0 :(得分:1)

(未经测试)但你的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
   }
 }