Alex Aghajanov
Alex Aghajanov

Reputation: 326

ViewDidLoad tableViewCell alpha issues?

I have a static table view:

enter image description here

When this screen is presented, I set the switch at the top in the off position in the viewDidLoad. I also set the tableViewCells, which I have connected via IBOutlet, to have an alpha of 0.5 (doesn't work), and the text fields to not respond to user interaction (works). When the switch at the top is turned on, the alpha of the tableViewCells (should be) is restored, and user interaction is re-enabled. This works. When the switch is turned off, the alpha of the cells goes back to 0.5 and the text field user interaction is set to false again. This whole line works. Basically, the switch works flawlessly, but the viewdidload does not. So, I'm wondering: why doesn't setting the alpha of the tableViewCells work in the viewDidLoad? I am certainly making a mistake somewhere, but this seems simple enough and I can't figure it out. Here is the relevant code:

@IBOutlet var switchCell1: UITableViewCell! 
@IBOutlet var theSwitch1: UISwitch! 
@IBOutlet var countryCell1: UITableViewCell! 
@IBOutlet var cityCell1: UITableViewCell! 
@IBOutlet var locationCell1: UITableViewCell! 
@IBOutlet var mainLocationTextField: UITextField!
@IBOutlet var countryTextField1: UITextField!
@IBOutlet var cityTextField1: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    

    theSwitch1.isOn = false
    
    //these three lines don't work, it is not "faded" out, if that's the right word
    countryCell1.alpha = 0.5
    cityCell1.alpha = 0.5
    locationCell1.alpha = 0.5
    //these three lines work
    countryCell1.isUserInteractionEnabled = false
    cityCell1.isUserInteractionEnabled = false
    locationCell1.isUserInteractionEnabled = false

}


//this function is connected to "theSwitch" and works perfectly
@IBAction func switch1Toggled(){
    if theSwitch1.isOn{
        countryCell1.alpha = 1.0
        cityCell1.alpha = 1.0
        locationCell1.alpha = 1.0
        countryCell1.isUserInteractionEnabled = true
        cityCell1.isUserInteractionEnabled = true
        locationCell1.isUserInteractionEnabled = true
    }else{
        countryCell1.alpha = 0.5
        cityCell1.alpha = 0.5
        locationCell1.alpha = 0.5
        countryCell1.isUserInteractionEnabled = false
        cityCell1.isUserInteractionEnabled = false
        locationCell1.isUserInteractionEnabled = false
    }
}

I really don't get why using the .alpha isn't working for me in the viewDidLoad but it works in another function. By the way, I have also tried calling the switch1Toggled() in the viewDidLoad so the function sets the alpha, but that also for some reason doesn't work. Thanks in advance for your time.

Upvotes: 0

Views: 36

Answers (1)

DonMag
DonMag

Reputation: 77530

The UI elements have not been fully initialized in viewDidLoad().

You can set their initial states in viewDidLayoutSubviews().

Note that viewDidLayoutSubviews() is called more than once - in fact, it is called often when using a table view - so you'll want to set a Bool "flag" to only run your initialization once:

var firstTime: Bool = true

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    if firstTime {
        firstTime = false

        theSwitch1.isOn = false
        
        countryCell1.alpha = 0.5
        cityCell1.alpha = 0.5
        locationCell1.alpha = 0.5

        countryCell1.isUserInteractionEnabled = false
        cityCell1.isUserInteractionEnabled = false
        locationCell1.isUserInteractionEnabled = false
    }
    
}

Upvotes: 1

Related Questions