简体   繁体   English

如何在UITableView Swift底部插入一个新Row

[英]How to insert a new Row in bottom of the UITableView Swift

I use 我用

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

method to insert a new Row in a table. 在表中插入新行的方法。 But it appears from Top of the UITableView (by default). 但它出现在UITableView顶部(默认情况下)。

I couldn't find any option to insert rows from the bottom of the table, like on screenshot: 我找不到任何从表格底部插入行的选项,比如屏幕截图:

来自底部的新行

I tried use the UIEdgeInsetsMake : 我尝试过使用UIEdgeInsetsMake

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

but it broke all design when I scroll the table. 但是当我滚动桌子时,它打破了所有的设计。

My question is: how to insert a new rows from bottom of the table? 我的问题是:如何从表格底部插入新行?

UPD: It should looks like table moves from a keyboard View to NavigationBar when new row is added. UPD:当添加新行时,它应该看起来像从键盘视图移动到NavigationBar。

UPD2: Here is what I want: http://recordit.co/JoR7xuvvpG UPD2:这就是我想要的: http//recordit.co/JoR7xuvvpG

I did it with the help of 我是在帮助下做到的

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

But contentInset adds a big white field above the rows and when I scroll down it hides added rows. 但是contentInset在行上方添加了一个大的白色字段,当我向下滚动时,隐藏了添加的行。 Looks like they is hiding under the keyboardView. 看起来他们藏在keyboardView下面。 I want to prevent it. 我想阻止它。

Swift 3 斯威夫特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)
func insertRowsAtIndexPaths(indexPaths: [NSIndexPath],
withRowAnimation animation: UITableViewRowAnimation)

It is the method for appending the rows. 这是附加行的方法。 For inserting rows at bottom you need to give indexpath of the last row. 要在底部插入行,您需要提供最后一行的索引路径。

For Example: 例如:

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

Did you try using UITableViewRowAnimationBottom as the row animation parameter? 您是否尝试使用UITableViewRowAnimationBottom作为行动画参数?
Like so: (in swift) 像这样:(在swift中)

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

Or Objective-C: 或Objective-C:

[self insertRowsAtIndexPaths:indexPaths
            withRowAnimation:UITableViewRowAnimationBottom];

Thanks @raf for the suggestion: 感谢@raf的建议:

What you need to do then, is animate the whole UITableView in it's parent view up as you insert elements. 您需要做的是,在插入元素时,在其父视图中为整个UITableView设置动画。 If you're using autolayout , capture the Top constraint in a property, and change it's constant as you insert elements. 如果您正在使用autolayout ,请捕获属性中的Top约束,并在插入元素时更改它的constant Something like: 就像是:

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

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

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