简体   繁体   中英

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).

I couldn't find any option to insert rows from the bottom of the table, like on screenshot:

来自底部的新行

I tried use the 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.

UPD2: Here is what I want: 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.

Swift 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?
Like so: (in swift)

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

Or Objective-C:

[self insertRowsAtIndexPaths:indexPaths
            withRowAnimation:UITableViewRowAnimationBottom];

Thanks @raf for the suggestion:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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