2

I'm using an insertRowAtIndexPath function to insert a series of rows inside my tableview. However, the in-build animation styles (.Fade, .Top, .Bottom e.t.c.) do not fill my needs entirely.

I need to create my own custom animations, i know this can be done by overriding the the UITableViewRowAnimation, but i'm not sure how this should be handled.

Looking forward to your advices.

Appreciate any insights.

David Robertson
  • 1,561
  • 4
  • 19
  • 41

1 Answers1

3

Try This in your UITableView subclass.

override func insertRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation) {
    super.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)

    self.endUpdates()
    self.beginUpdates()

    for indexPath:NSIndexPath in indexPaths {

        let cell:UITableViewCell? = (super.cellForRowAtIndexPath(indexPath))

        if cell != nil {
            var frame:CGRect = (cell?.frame)!
            frame.origin.x = (cell?.frame.size.width)!
            cell?.frame = frame
            frame.origin.x = 0

            let animationBlock = { () -> Void in
                cell!.frame = frame;
            }

            if UIView.respondsToSelector(Selector("animateWithDuration(duration: , delay: , usingSpringWithDamping dampingRatio: , initialSpringVelocity velocity: , options: , animations: , completion: ")) {
                // UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options:0.0, animations: animationBlock, completion:nil)
                UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: animationBlock, completion: nil)
            } else {
                UIView.animateWithDuration(0.3, delay: 0.0, options:UIViewAnimationOptions.CurveEaseIn, animations: animationBlock, completion: nil)
            }
        }
    }
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
johny kumar
  • 1,270
  • 2
  • 14
  • 24
  • Why the `endUpdates()` and `beginUpdates()`? – NRitH Sep 04 '15 at 13:17
  • @NRitH its just swift converted code of link which i suggest to follow.i only converted it to swift. – johny kumar Sep 04 '15 at 15:20
  • thank you for the great effort. although, i've got an issue while implementing the code. i don't have an `UITableView` class, I'm just using the `UITableViewDatasource` and the `UITableViewDelegate` as well as `UIViewController` for basic table functions - when i try to add an `UITableView` class or replace the current `UIViewController` i get a crash. – David Robertson Sep 04 '15 at 16:19
  • Create a class while creating choose subclass of UITableVIew and after that add this method in .m file then go to your storyboard or xib then select tableview and go in Identity Inspector and assign your class for tableview. – johny kumar Sep 05 '15 at 04:35
  • i've been able to make it work with a few tweaks - [NSIndexPath] should be changed to [AnyObject] e.t.c. in the code. Yet the issue is - it animates the insertion of a whole block, i was looking for the insertion of every single cell. Would that be possible? – David Robertson Sep 05 '15 at 10:05
  • Besides this animation only allows me to control the duration (its ease out from left all the time) and applies only for visible rows. – David Robertson Sep 05 '15 at 10:44
  • you can pass one indexpath for single row in it with single object in array and for duration 0.3 is given change value according to your need. – johny kumar Sep 05 '15 at 10:50
  • sounds reasonable, but i'm not sure how to do it. currently i have multiple objects in the array. – David Robertson Sep 05 '15 at 11:07
  • create a temp array in for loop for iterating your objects and add single object in temp array call insert method in for loop then delete object from temp array with the for loop. – johny kumar Sep 05 '15 at 11:11
  • that would be a good idea if i had a blank slate uitableview, but all my cell insertion logic is tied up to the array of arrays. this would require reworking the project entirely. need to find a different solution ( – David Robertson Sep 05 '15 at 12:12
  • dont do like that it will effect your whole project whereever you use that array.do same as i have wrote above. – johny kumar Sep 05 '15 at 12:21
  • how about we track the specific indexPath range of the inserted cells and use it instead of AnyObject? then we can either use a for loop from your code or chain animation actions for every single row one after another. that might work. – David Robertson Sep 05 '15 at 12:47
  • it depends on the user action or your projects functionality where you want to insert cell you will get index path. – johny kumar Sep 06 '15 at 07:19
  • thank you for great help. so far i haven't been able to find a better solution of overriding the insertRowsAtIndexPaths function. yet still struggling with -1) Inserting every row separately with animation -2) Keeping the animation from dissapearing when the cells are hidden from view (during scroll) – David Robertson Sep 06 '15 at 11:43
  • there is another method willDisplaycell whenever a row is shown on the screen you can add same functionality of animation in this.it will show for every cell. – johny kumar Sep 06 '15 at 12:41
  • the issue with willDisplaycell is that it always reloads a cell with animation, each time you scroll. i need it just once. – David Robertson Sep 06 '15 at 21:54