Parth Bhuva
Parth Bhuva

Reputation: 877

UIView.animateWithDuration stops after scrolling uitableviewcell

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

Upvotes: 3

Views: 1919

Answers (5)

Parth Bhuva
Parth Bhuva

Reputation: 877

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

Upvotes: -1

Priyanka Wadher Mistry
Priyanka Wadher Mistry

Reputation: 464

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

prepareForReuse is called every time whenever dequeueReusableCellWithIdentifier is called.

Upvotes: 1

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

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

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27428

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

Upvotes: 1

Ali Abbas
Ali Abbas

Reputation: 4277

You should put your code in this UITableViewDelegate method :

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

Upvotes: 0

Related Questions