2016-01-21 77 views
2

的底部插入新行我用如何在UITableView的斯威夫特

func insertRowsAtIndexPaths(indexPaths: [NSIndexPath], 
withRowAnimation animation: UITableViewRowAnimation) 

方法在表中插入新行。但它從UITableView的頂部出現(默認情況下)。

我找不到任何選項從表底上的截圖插入行,如:

New Rows from Bottom

我試圖用UIEdgeInsetsMake

self.tableView.contentInset = UIEdgeInsetsMake(tableView.frame.height,0,0,0) 

,但它打破了當我滾動桌子時所有的設計。

我的問題是:如何從表底部插入新的行?

UPD:它應該看起來像添加新行時從鍵盤視圖移動到NavigationBar。

UPD2:這是我想要的:http://recordit.co/JoR7xuvvpG

我的

self.tableView.contentInset = UIEdgeInsetsMake(tableView.frame.height,0,0,0) 

的幫助下做到了,但contentInset添加行上面一個大的白色場,當我向下滾動,它隱藏添加行。看起來他們隱藏在keyboardView下面。我想阻止它。

回答

0

感謝@raf您的建議:

你需要做的是什麼,是動畫整個UITableView在它的父查看了您插入的元素。如果您使用的是autolayout,請捕獲屬性中的Top約束,並在插入元素時將其更改爲constant。喜歡的東西:

topConstraint.constant = newValue 
UIView.animateWithDuration(0.5, animations: { 
    self.layoutIfNeeded() 
}) 
+0

你能給我這個代碼的Objective-C等價物嗎? – Illep

+0

不幸的是:(我只能迅速:) –

0

您是否嘗試使用UITableViewRowAnimationBottom作爲行動畫參數?
像這樣:(在SWIFT)

tableView.insertRowsAtIndexPaths([indexPath], 
    withRowAnimation: UITableViewRowAnimation.Bottom) 

或Objective-C:

[self insertRowsAtIndexPaths:indexPaths 
      withRowAnimation:UITableViewRowAnimationBottom]; 
+0

它不工作,因爲它是唯一的動畫。行使用.Bottom動畫從UITableView的頂部出現。 我想這行應該從表格底部出現。 它應該看起來像一個表,從鍵盤出現,並添加新行時移動到頂部。 –

+0

好吧,請確保您添加到數據源的「底部」,並且新行的索引比行上最後一個項目的索引高一個。 – raf

+0

不幸的是,不起作用。我想這樣做:http://recordit.co/JoR7xuvvpG –

3
func insertRowsAtIndexPaths(indexPaths: [NSIndexPath], 
withRowAnimation animation: UITableViewRowAnimation) 

它是用於附加行的方法。爲了在底部插入行,你需要給最後一行的索引路徑。

例如:

var IndexPathOfLastRow = NSIndexPath(forRow: self.array.count - 1, inSection: 0) 
self.tableView.insertRowsAtIndexPaths([IndexPathOfLastRow], withRowAnimation: UITableViewRowAnimation.Left) 
+0

感謝您的建議,但這不能解決我的問題。對不起,我的英文,也許我說錯了。看,我想要的:http://recordit.co/JoR7xuvvpG我做了它的幫助:self.tableView.contentInset = UIEdgeInsetsMake(tableView.frame.height,0,0,0)但是當我向下滾動,你可以看到添加的行上方的大白色字段。我想阻止它。 –

6

斯威夫特3

TableView.beginUpdates() 

    let indexPath:IndexPath = IndexPath(row:(self.yourArray.count - 1), section:0) 

    TableView.insertRows(at: [indexPath], with: .left) 

    TableView.endUpdates() 

    TableView.scrollToRow(at: indexPath, at: .bottom, animated: true)