setting a row height to 0, for selected rows (swift)

2.6k views Asked by At

In my app, I have "groups" of species displayed on a UITableView.

Some groups are free and some are in-App purchase.

The groups that are an in-app purchase have a UIImage of a padlock on that row set up in the cellForRowAtIndexPath.

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell: UITableViewCell       = tableView.dequeueReusableCell(withIdentifier: Resource.SpeciesCell)!

        let specieImage: UIImageView    = cell.viewWithTag(Resource.SpeciesImageTag) as! UIImageView
        let specieName: UILabel         = cell.viewWithTag(Resource.SpeciesNameTag) as! UILabel
        let specieGenus: UILabel        = cell.viewWithTag(Resource.SpeciesGenusTag) as! UILabel
        let specieFamily: UILabel       = cell.viewWithTag(Resource.SpeciesFamilyTag) as! UILabel

        specieName.text     = self.species[(indexPath as NSIndexPath).row].specie
        specieFamily.text   = ""
        specieGenus.text    =  AppDelegate.getRLDatabase().getSubGroupName_BySpecieName(specieName.text!)


        let padLock: UIImageView        = cell.viewWithTag(Resource.SpeciesCategoryLabelTag) as! UIImageView

        padLock.image = UIImage(named: "PadlockIcon")
        padLock.alpha = 0.7

        let fishesPurchased = UserDefaults.standard.bool (forKey: "ReefLife5Fishes")
        let sharksPurchased = UserDefaults.standard.bool (forKey: "ReefLife6Sharks")
        let turtlesPurchased = UserDefaults.standard.bool (forKey: "ReefLife8Turtles")
        let seahorsesPurchased = UserDefaults.standard.bool (forKey: "ReefLife9Seahorses")
        let vertebratesPurchased = UserDefaults.standard.bool (forKey: "ReefLife3Vertebrates")
        let fullPurchased = UserDefaults.standard.bool (forKey: "ReefLife1Full")

        padLock.isHidden = false
        specieImage.alpha = 1.0

        let speciesGroup = self.species[(indexPath as NSIndexPath).row].group

        if fullPurchased == true {

            padLock.isHidden = true
            specieImage.alpha = 1.0

        } else if speciesGroupVertebratesArray.contains(speciesGroup) {

            if vertebratesPurchased == true {

                padLock.isHidden = true
                specieImage.alpha = 1.0

            } else {

                if speciesGroup == "Fish" {
                    if fishesPurchased == true{
                        padLock.isHidden = true
                        specieImage.alpha = 1.0

                    } else{
                        specieImage.alpha = 0.5
                    }
                } else if (speciesGroup == "Sharks" || speciesGroup == "Rays" ) {
                    if sharksPurchased == true{
                        padLock.isHidden = true
                        specieImage.alpha = 1.0

                    } else{
                        specieImage.alpha = 0.5
                    }
                } else if speciesGroup == "Syngnathiformes" {
                    if seahorsesPurchased == true{
                        padLock.isHidden = true
                        specieImage.alpha = 1.0

                    } else{
                        specieImage.alpha = 0.5
                    }
                } else if speciesGroup == "Reptilia" {
                    if turtlesPurchased == true{
                        padLock.isHidden = true
                        specieImage.alpha = 1.0

                    } else{
                        specieImage.alpha = 0.5
                    }
                }
            }

        }
 .... 

I have an option settings to "hide locked items" based on a UISwitch

When the user selects this, I want to hide the locked groups ( or non purchased items). I figured the best way to achieve this would be to hide the specific rows, by setting the row height to 0.

I am struggling to set the row height to 0 using the indexPath.row as it the indexPath.row does not accept a CGFloat command.

Suggestions?

EDIT: Realize I need to use the heightForRowAt indexPath: IndexPath function, but I have having a difficult time assigning it to a indexPath.row.

Maybe a better question would be: How can I set it up so that I know which indexPath.row has "padLock.isHidden == false".

2

There are 2 answers

2
Ron Diel On BEST ANSWER

Row height is set in a separate delegate function, not the cellForRowAt setup function. Try something like this:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    rowHeight = [your default row height]

    record = frc.object(at: indexPath) as [YourEntity]

    if record.[what indicates locked] == true
    {
        rowHeight = 0
     }

    return CGFloat(rowHeight)
}
0
Marco On

You can use the method tableView(height forRow}.)

Then(for reloading the layout of the tableView), use the method: tableView.beginUpdates and then tableView.endingUpdates