3

i have added a infinite animation in uiTableViewCell which just blinks a UILabel inside the table view cell.

my problem is, when i scrolls the tableview it just stops the blinking

my code is

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("TripListCell", forIndexPath: indexPath) as! TripListCell
    
    let trip = tripList[indexPath.section]
    cell.lblTripDirection.textColor = UIColor(red: 51/255, green: 210/255, blue: 123/255, alpha: 1.0)
    
    UIView.animateWithDuration(0.5, delay: 0.0, options: [.CurveEaseInOut, .Repeat, .Autoreverse, .AllowUserInteraction], animations: {
        
        cell.lblTripDirection.alpha = 0.0
        }, completion: {
            bool in
        cell.lblTripDirection.alpha = 1.0
        cell.lblTripDirection.textColor = UIColor.blackColor()
    })
    return cell
}

Update:

UIView.commitAnimations() before returning the cell worked for me. Thank you everyone :)

Parth Bhuva
  • 857
  • 6
  • 26

5 Answers5

1

You can override prepareForReuse method of the UITableViewCell in your custom cell TripListCell.

prepareForReuse is called every time whenever dequeueReusableCellWithIdentifier is called.

halfer
  • 19,824
  • 17
  • 99
  • 186
1

It is because cellForRowAtIndexPath resuse the same cell to display other data. so you shouln't write your animation in cellForRowAtIndexPath.

You should try to write in awakeFromNib of custom cell class or you should use willDisplayCell method to write animation.

hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

You should put your code in this UITableViewDelegate method :

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath);
Ali Abbas
  • 4,247
  • 1
  • 22
  • 40
0

TableViews reuse their cells so inevitably your label stop their animation.

You can restore the reused cell to its default state either in prepareForReuse: on the custom table view cell or in the tableView:willDisplayCell:forRowAtIndexPath: delegate method.

In this case you use UIView animations, you only need to change the value of the animated property to cancel the animation and return to the default state.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    //declaring the cell variable again

    var cell: TripListCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TripListCell
    UIView.performWithoutAnimation {
        cell.lblTripDirection.layoutIfNeeded()
    }
    // do your animation..
    ...
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
-1

UIView.commitAnimations() before returning the cell worked for me.

Parth Bhuva
  • 857
  • 6
  • 26