Search code examples
iosswiftcell

Hide / Show @IBOulet @IBAction on load in Cell


I am trying to only let the button show if the value of something is available.

@IBAction func instagramButtonHide(_ sender: Any) {
    if(self.thisLens.instagramURL.isEmpty){
        addInstagramButton.isHidden = true
    }else{
        let url = URL(string: thisLens.instagramURL)!
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

The code is working but only if you click on it. So I thought let's place it in the viewDidAppear so it loads when the view loads but since it is in a cell I can't do this.

So if the value of instagramURL is empty I don't want the button to show up.

I feel there is an easy fix but I can't find it.

@IBOutlet weak var addFacebookButton: UIButton!
@IBOutlet weak var shareButton: UIButton!

var thisLens: WordPressPost!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code


    addFacebookButton.isHidden = self.thisLens.facebookURL.isEmpty
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

@IBAction func favoriteButtonPressed(_ sender: UIButton) {

}

@IBAction func facebookButtonClicked(_ sender: Any) {
    let url = URL(string: thisLens.facebookURL)!
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

And the other one

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var thisLens: WordPressPost!

    var numberOfPosts = posts.count
    if searchController.isActive {
        numberOfPosts = filteredPosts.count
    }
    if indexPath.row != numberOfPosts {
        var thisPost: WordPressPost
        if searchController.isActive {
            thisPost = filteredPosts[indexPath.row]
        }
        else {
             thisPost = posts[indexPath.row]
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "lensCell", for: indexPath) as! LensCell

        cell.thisLens = thisPost
        if thisPost.authorName == "" {
            cell.authorNameLabel.text = "No Author"
        }
        else {
            cell.authorNameLabel.text = thisPost.authorName
        }
        if thisPost.title == "" {
            cell.lensNameLabel.text = "No Title"
        }
        else {
            cell.lensNameLabel.text = thisPost.title
        }

        if thisPost.image != nil {
            cell.snapCodeImageView.image = thisPost.image
            cell.imageLoadingIndicator.stopAnimating()
        }
        else {
            cell.snapCodeImageView.image = nil
            cell.imageLoadingIndicator.startAnimating()
            downloadImage(url: URL(string: thisPost.imageURL)!, cellIndexPath: indexPath, wasSearchControllerActive: searchController.isActive, typingUID: currentTypingUID)
        }
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "loadingCell", for: indexPath) as! LoadingCell
        cell.loadingIndicator.startAnimating()
        return cell
    }
}

Solution

  • Then do this inside awakeFromNib of the cell

    addInstagramButton.isHidden = self.thisLens.instagramURL == nil
    

    or inside cellForRowAt

    let item = ///
    cell.addInstagramButton.isHidden = item.instagramURL == nil