ios 如何在 UITableView Swift 底部插入新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34932050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a new Row in bottom of the UITableView Swift
提问by Oleksandr Fisun
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:添加新行时,表格应该从键盘视图移动到导航栏。
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. Looks like they is hiding under the keyboardView. I want to prevent it.
但是 contentInset 在行上方添加了一个大的白色字段,当我向下滚动时,它会隐藏添加的行。看起来他们隐藏在键盘视图下。我想阻止它。
采纳答案by Oleksandr Fisun
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. If you're using autolayout
, capture the Top constraint in a property, and change it's constant
as you insert elements. Something like:
然后你需要做的是UITableView
在你插入元素时在它的父视图中设置整体动画。如果您使用的是autolayout
,请在属性中捕获 Top 约束,并constant
在插入元素时更改它。就像是:
topConstraint.constant = newValue
UIView.animateWithDuration(0.5, animations: {
self.layoutIfNeeded()
})
回答by Vimal Saifudin
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)
回答by Samraan Khaan
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)
回答by raf
Did you try using UITableViewRowAnimationBottom
as the row animation parameter?
Like so: (in swift)
您是否尝试将其UITableViewRowAnimationBottom
用作行动画参数?
像这样:(迅速)
tableView.insertRowsAtIndexPaths([indexPath],
withRowAnimation: UITableViewRowAnimation.Bottom)
Or Objective-C:
或目标-C:
[self insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationBottom];