ios - 来自 UITableView 的 View 消失

标签 ios swift uitableview uiview

我有一个显示 14 个不同 View 的自定义单元格。根据单元格接收到的数据,它应该显示等于某些数据的 .count 的 View ,并且颜色应该根据它是什么数据而改变。

例如:

如果它接收三种类型的数据,它应该只显示 3 个 View 。可以是冰淇淋、糖果、棉花糖。然后它将显示三个 View ,分别为橙色(冰淇淋)、蓝色(糖果)、绿色(棉花糖)

我得到了很好的效果,我对此感到很兴奋。问题是,如果我有一个单元格显示三个 View 并向下滚动到一个只包含一个 View 的单元格(因为数据只有一个),那么当我再次向上滚动到第一个原本应该显示三个 View 的单元格时,它是只显示 1,第一个..

我有一个例子:

我在 Storyboard中的自定义单元格是这样的,绿色和黑色框是不同的 View enter image description here

这是 6 种数据的样子: enter image description here

当我向下滚动到包含一个 View 的单元格时,具有 6 个 View 的单元格之后将如下所示:

enter image description here

这里是一些相关的代码:

让我解释一下代码。在我的数据库中,每个帖子都有一个类别,它是 1 或 2。这就是代码在 update.category 中搜索的内容。如果它是类别 1,则它只是纯文本。如果它是类别 2(或其他),它应该显示 View ,所以我实际上必须在我的 UITableViewController 中设置单元格类型。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let update = updates[indexPath.row]

    if update.category == 1 {

        let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell

        cell.nameButton.setTitle(update.addedByUser, forState: .Normal)

        return cell

    } else {

        let cellSugar:newSugarTableViewCell = tableView.dequeueReusableCellWithIdentifier("CellSugar", forIndexPath: indexPath) as! newSugarTableViewCell

        cellSugar.nameButton.setTitle(update.addedByUser, forState: .Normal)

        let sugarArray = update.content.componentsSeparatedByString("--")

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            cellSugar.sugarViewOne.layer.cornerRadius = cellSugar.sugarViewOne.frame.size.width/2
            cellSugar.sugarViewOne.clipsToBounds = true
            cellSugar.sugarViewOne.layer.borderColor = UIColor.whiteColor().CGColor
            cellSugar.sugarViewOne.layer.borderWidth = 2.0

            cellSugar.sugarViewTwo.layer.cornerRadius = cellSugar.sugarViewTwo.frame.size.width/2
            cellSugar.sugarViewTwo.clipsToBounds = true
            cellSugar.sugarViewTwo.layer.borderColor = UIColor.whiteColor().CGColor
            cellSugar.sugarViewTwo.layer.borderWidth = 2.0
        })


        if sugarArray.count == 1 {

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let seperateSugarArray = sugarArray[0].componentsSeparatedByString("#")

                if seperateSugarArray[4] == "Candy" {
                    cellSugar.sugarViewOne.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå

                } else if seperateSugarArray[4] == "Ice cream" {
                    cellSugar.sugarViewOne.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå

                } else if seperateSugarArray[4] == "Marshmellows" {
                    cellSugar.sugarViewOne.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis

                }

                cellSugar.sugarViewTwo.hidden = true
                cellSugar.sugarViewThree.hidden = true
                cellSugar.sugarViewFour.hidden = true
                cellSugar.sugarViewFive.hidden = true
                cellSugar.sugarViewSix.hidden = true
                cellSugar.sugarViewSeven.hidden = true
                cellSugar.sugarViewEight.hidden = true
                cellSugar.sugarViewNine.hidden = true
                cellSugar.sugarViewTen.hidden = true
                cellSugar.sugarViewEleven.hidden = true
                cellSugar.sugarViewTwelve.hidden = true
                cellSugar.sugarViewThirteen.hidden = true
                cellSugar.sugarViewFourteen.hidden = true
            })


        } else if sugarArray.count == 2 {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                let seperateSugarArray = sugarArray[0].componentsSeparatedByString("#")
                let seperateSugarArrayTwo = sugarArray[1].componentsSeparatedByString("#")

                if seperateSugarArray[4] == "Candy" {
                    cellSugar.sugarViewOne.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå

                } else if seperateSugarArray[4] == "Ice cream" {
                    cellSugar.sugarViewOne.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå

                } else if seperateSugarArray[4] == "Marshmellows" {
                    cellSugar.sugarViewOne.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis

                }

                if seperateSugarArray[4] == "Candy" {
                    cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 97.0/255.0, green: 194.0/255.0, blue: 231.0/255.0, alpha: 1.0) // Blå

                } else if seperateSugarArray[4] == "Ice cream" {
                    cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 35.0/255.0, green: 117.0/255.0, blue: 147.0/255.0, alpha: 1.0) // Mørke grå/blå

                } else if seperateSugarArray[4] == "Marshmellows" {
                    cellSugar.sugarViewTwo.backgroundColor = UIColor(red: 75.0/255.0, green: 212.0/255.0, blue: 159.0/255.0, alpha: 1.0) // Tyrkis

                }


                cellSugar.sugarViewThree.hidden = true
                cellSugar.sugarViewFour.hidden = true
                cellSugar.sugarViewFive.hidden = true
                cellSugar.sugarViewSix.hidden = true
                cellSugar.sugarViewSeven.hidden = true
                cellSugar.sugarViewEight.hidden = true
                cellSugar.sugarViewNine.hidden = true
                cellSugar.sugarViewTen.hidden = true
                cellSugar.sugarViewEleven.hidden = true
                cellSugar.sugarViewTwelve.hidden = true
                cellSugar.sugarViewThirteen.hidden = true
                cellSugar.sugarViewFourteen.hidden = true


            })

        }

      return cellSugar
    }
}

我希望你能理解并帮助我,因为这很烦人:-)

最佳答案

您必须为每个条件更新所有元素。 问题的原因:表格 View 正在重用单元格,因此如果您有一个单元格显示 5 个标签,并且在滚动后您有另一个单元格有一个 3 而另一个 2 被隐藏,当您再次向上滚动时,您必须使它们可见表格 View 再次使用带有 2 个隐藏标签的 View 。 在 CellForRowAtIndexPath 中添加每个条件中缺少的标签 或者在您的自定义 UItableViewCell 类中添加方法 prepareForReuse 并使所有标签可见。

关于ios - 来自 UITableView 的 View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39688895/

相关文章:

ios - UITableViewCell drawRect 底部边框在 insertRow 上消失

ios - swift 为 collectionView 正确关闭键盘

swift - 点击时增加/减少表格 View 单元格的大小 - Swift

Swift:PlayGround 上的 For-in 循环非包容性上层

ios - 来自搜索的 TableView 数据未正确更新

iphone - 在 UITableview 中自动向下和向上滚动

ios - 将 Storyboard View 中的所有内容移动到 ScrollView 中

ios - dyld : Library not loaded: @rpath/libswiftCore. dylib 仅在 Xcode 9 上

ios - 相同的代码在一个项目中有效,在另一个项目中无效

ios - 如何使用 Swift 在 iOS 中添加空白 UITableViewCell?