簡體   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