2016-12-28 67 views
1

在我的應用程序中,我在UITableView上顯示了物種的「組」。將行高設置爲0,對於選定的行(swift)

有些羣組是免費的,有些羣組是在應用內購買的。

應用內購買的羣組在cellForRowAtIndexPath中設置的該行上具有掛鎖的UIImage。

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

     } 
.... 

我有一個選項設置爲「隱藏鎖定的項目」基於一個UISwitch

當用戶選擇這個,我想隱藏鎖定小組(或不購買的物品)。我想通過將行高設置爲0可以隱藏特定的行。

我很努力地將行高設置爲0,使用indexPath.row作爲它的indexPath.row不接受CGFloat命令。

對此提出建議?編輯: 實現我需要使用heightForRowAt indexPath:IndexPath函數,但我有一個困難的時間分配給indexPath.row。

也許更好的問題是:我如何設置它,以便我知道哪個indexPath.row具有「padLock.isHidden == false」。

回答

2

行高設置在單獨的委託函數中,而不是cellForRowAt設置函數。嘗試這樣的事情:

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

謝謝,我一直在嘗試在heightForRow裏面,但是你的想法比我一直在嘗試的要多得多。上述唯一的問題是我無法弄清楚frc代表什麼? –

+0

那就是我所謂的fetchedResultsController:private var frc = NSFetchedResultsController () –