tableView图像随机消失

时间:2016-12-07 13:25:41

标签: ios uitableview swift3

我正在通过IAP将我付费的应用修改为免费。

某些群组类型是免费的,有些群体需要付费。

在tableView控制器上,有一个挂锁UIImageView,如果它是锁定组的一部分,则显示每个“锁定”项目。

当有人购买该组或全部时,该挂锁意味着消失。这个挂锁不是代码的一部分,阻止用户看到细节,因为我在其他地方有代码,它工作正常。

最初,“挂锁”准确显示。但是,如果我在tableView上向上和向下滚动,挂锁将随机消失。

如何防止这种情况?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    //
    //  We need to fetch our reusable cell.
    //
    let cell: UITableViewCell       = tableView.dequeueReusableCell(withIdentifier: Resource.SpeciesCell)!


    //
    //  Get all of the components of the cell.
    //
    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

    //  Set our name, family and genus labels from our data.
    //
    specieName.text     = self.species[(indexPath as NSIndexPath).row].specie

    specieFamily.text   = ""

    specieGenus.text    = self.species[(indexPath as NSIndexPath).row].speciesSubgroup




    // **** Adding Padlock Begins HERE  ****

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

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


    let speciesGroupFreeArray: Array<String>
    let speciesGroupVertebratesArray: Array<String>
    let speciesGroupInvertebratesArray: Array<String>

    speciesGroupFreeArray = ["eels", "Mammals", "Annelids", "Bivalvians", "Cephalopods", "Cool oddities", "Crustacean", "Echinoderms", "Hydrozoans", "Isopods"]
    speciesGroupVertebratesArray = ["Fish", "Sharks", "Rays", "Reptilia", "Syngnathiformes"]
    speciesGroupInvertebratesArray = ["Corals", "Gastropods"]

    let fishesPurchased = UserDefaults.standard.bool (forKey: "ReefLife5Fishes")
    let sharksPurchased = UserDefaults.standard.bool (forKey: "ReefLife6Sharks")
    let nudisPurchased = UserDefaults.standard.bool (forKey: "ReefLife7Nudis")
    let turtlesPurchased = UserDefaults.standard.bool (forKey: "ReefLife8Turtles")
    let seahorsesPurchased = UserDefaults.standard.bool (forKey: "ReefLife9Seahorses")
    let coralsPurchased = UserDefaults.standard.bool (forKey: "ReefLife4Corals")
    let vertebratesPurchased = UserDefaults.standard.bool (forKey: "ReefLife3Vertebrates")
    let invertebratesPurchased = UserDefaults.standard.bool (forKey: "ReefLife2Invertebrates")
    let fullPurchased = UserDefaults.standard.bool (forKey: "ReefLife1Full")


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

    if fullPurchased == true {

        padLock.isHidden = true

    } else if speciesGroupVertebratesArray.contains(categoryName) {

        if vertebratesPurchased == true {

            padLock.isHidden = true

        } else {

            if categoryName == "Fish" {
                if fishesPurchased == true{
                    padLock.isHidden = true
                } else{

                }
            } else if (categoryName == "Sharks" || categoryName == "Rays" ) {
                if sharksPurchased == true{
                    padLock.isHidden = true
                } else{

                }
            } else if categoryName == "Syngnathiformes" {
                if seahorsesPurchased == true{
                    padLock.isHidden = true
                } else{

                }
            } else if categoryName == "Reptilia" {
                if turtlesPurchased == true{
                    padLock.isHidden = true
                } else{

                }
            }
        }

    } else if speciesGroupInvertebratesArray.contains(categoryName) {

        if invertebratesPurchased == true {
            padLock.isHidden = true

        } else {

            if categoryName == "Corals" {
                if coralsPurchased == true{
                    padLock.isHidden = true
                } else{

                }
            } else if categoryName == "Gastropods" {
                if nudisPurchased == true{
                    padLock.isHidden = true
                } else{

                }
            }
        }
    }

    if speciesGroupFreeArray.contains(categoryName) {

        padLock.isHidden = true
    }





    // ****  Adding Padlock Ends HERE  ****


    //
    //  We now need to set our photo. First, we need to fetch the photo based on our specie
    //  id.
    //
    let photos = AppDelegate.getRLDatabase().getSpeciePhotos(self.species[(indexPath as NSIndexPath).row].id)

    //
    //  Set the image for our UIImageView.
    //
    if photos.isEmpty != true
    {
        specieImage.clipsToBounds = true
        specieImage.contentMode = .scaleAspectFill
        specieImage.image = UIImage(named: photos[0].name)


        //            specieImage.image = UIImage(named: photos[0].name)?.resizeTo(specieImage.bounds)
    }
    else
    {
        specieImage.image = UIImage(named: Resource.UnknownImage)?.resizeTo(specieImage.bounds)
    }

    //
    //  Return our new cell.
    //
    return cell
}

2 个答案:

答案 0 :(得分:2)

请尝试替换此

if speciesGroupFreeArray.contains(categoryName) {
    padLock.isHidden = true
}

padLock.isHidden = speciesGroupFreeArray.contains(categoryName)

答案 1 :(得分:1)

您的代码只会设置padlock.isHidden = true。如果你得到一个已经隐藏挂锁的再生牢房,就没有什么可以取消隐藏了。

您需要在所有情况下明确设置挂锁isHidden状态。

鉴于您设置isHidden属性的代码有些扭曲,修复当前代码的最简单方法是在padlock.isHidden = false方法的开头设置cellForRowAt()。然后,如果没有任何案例设置为隐藏,则它将始终是非禁止的,即使在您重新配置隐藏挂锁的回收单元格的情况下也是如此。

编辑:

这是在iOS中管理表视图和集合视图的关键要点。始终假设所有视图都设置为非默认状态的单元格。您的代码始终需要将每个视图显式设置为特定状态。

将一个表格视图单元格视为医生办公室的纸质患者信息表格,办公室会回收表格,而不会删除前一位患者留下的信息。您总是要擦除前一位患者留下的信息。如果您对花生没有过敏,仅仅不检查那个盒子是不够的。您必须删除以前的复选标记和其他信息,否则之前患者的信息将与您的信息混淆。

这就是你的padlock.isHidden状态所发生的事情。你假设它是在默认状态下开始的,但它可能没有。

相关问题