Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

New Rows from Bottom

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.

like image 215
Oleksandr Fisun Avatar asked Jan 21 '16 18:01

Oleksandr Fisun


Video Answer


2 Answers

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)
like image 161
Vimal Saifudin Avatar answered Oct 08 '22 04:10

Vimal Saifudin


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)
like image 40
Samraan Khaan Avatar answered Oct 08 '22 02:10

Samraan Khaan