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

后端 未结 4 1308
傲寒
傲寒 2021-01-12 18:10

I use

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

method to insert a new Ro

相关标签:
4条回答
  • 2021-01-12 18:59
    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)
    
    0 讨论(0)
  • 2021-01-12 19:00

    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];
    
    0 讨论(0)
  • 2021-01-12 19:02

    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)
    
    0 讨论(0)
  • 2021-01-12 19:02

    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() 
    })
    
    0 讨论(0)
提交回复
热议问题