Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton | setTitle taking huge amount of time even in main thread

I’m reopening this question because my last one wast flagged as duplicate even if it’s actually not ! It’s the same problem but the solutions are not working with my code. I’m using swift 2.

So my problem is, as the title says : I have a UIButton in a tableViewCell and when I use the method « setTitle », it takes from 10 to 60 seconds to update the title. At the same time I’m using « addTarget » and it works instantly. So the title should also update. My button is set as « custom » in my storyboard.

When the view is loading I’m running the following code :

/* viewDidLoad */

override func viewDidLoad() {
    super.viewDidLoad() 
    boolAlready = false 
    findParticipation() 
}

/* findParticipation */

func findParticipation() {
    // After server request response :
    boolAlready = true 
    self.tableView.reloadData() 
}

/* cellForRowAtIndexPath */

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellActions = tableView.dequeueReusableCellWithIdentifier(informationsCellArray[indexPath.row], forIndexPath: indexPath) as     ! eventDetailsAction

    if boolAlready { 
         cellActions.foundParticipation
    } else { 

        cellActions.btnParticipate.setTitle("...", forState: UIControlState.Normal)
        cellActions.btnParticipate.addTarget(self, action: « deleteParticion", forControlEvents: .TouchUpInside)
} 

/* In my custom cell */

func foundParticipation () {
    self.btnParticipate.setTitle("Annuler", forState: UIControlState.Normal)
    self.btnParticipate.addTarget(self, action: "deleteParticipation", forControlEvents: .TouchUpInside)
}

Different things I found on forums that didn’t worked :

  1. Putting my settitle action around dispatch_async(dispatch_get_main_queue()) {}

  2. Setting title for all differents UIControlStates

  3. Using setAttributedTitle()

  4. Using self.btnParticipate.setNeedsLayout() and self.btnParticipate.layoutIfNeeded() after the setTitle

  5. Disabling the button before and enable it after the setTitle

  6. self.addSubview(self.btnParticipate)

  7. Changing the title in titleLabel.text

  8. Doing everything said previously in the parent viewController using cellActions.btnParticipate

  9. UIView.performWithoutAnimation { self.btnParticipate.setTitle("Annuler", forState: .Normal) }

I’m now stuck and can’t find a solution for that.

like image 292
Nicolas Meienberger Avatar asked Dec 30 '25 08:12

Nicolas Meienberger


1 Answers

You can try to refer to this answer https://stackoverflow.com/a/29633647/4478037

Make sure your button is a "custom" button and not a "system" button.

like image 54
IMike17 Avatar answered Dec 31 '25 22:12

IMike17



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!