Uviewcell subview color

I'm having a weird problem (in my opinion;)) considering the cell subclass uitableview. Code for my subclass:

class SubLevelTableCell: UITableViewCell {
    var subLevelLabel:UILabel
    var subLevelBack:UIView
    var subLevelScore:UIImageView

    override init(style: UITableViewCellStyle, reuseIdentifier: String!)
    {
        self.subLevelBack = UIView()
        self.subLevelLabel = UILabel()
        self.subLevelScore = UIImageView()

        super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)

        self.subLevelLabel.textColor = whiteColor

        self.subLevelLabel.font = UIFont(name: subLevelLabel.font.fontName, size: 20)

        self.addSubview(self.subLevelBack)
        self.subLevelBack.addSubview(self.subLevelLabel)
        self.subLevelBack.addSubview(self.subLevelScore)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        self.subLevelBack.frame = CGRectMake(10, 10, self.bounds.size.width-20, self.bounds.size.height-20)
        self.subLevelLabel.frame = CGRectMake(5, 0, subLevelBack.frame.size.width/2-10, subLevelBack.frame.size.height)
        self.subLevelScore.frame = CGRectMake(subLevelBack.frame.size.width-120, 15, 100, subLevelBack.frame.size.height-30)
    }

}

      

Now I create these cells in my view controller like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:SubLevelTableCell? = SubLevelTable?.dequeueReusableCellWithIdentifier("Cell") as? SubLevelTableCell
        if (cell == nil)
        {
            cell = SubLevelTableCell(style: UITableViewCellStyle.Subtitle,
                reuseIdentifier: "Cell")
            cell!.backgroundColor = grayColor
        }
        if (indexPath.row == 0){nextLevelCanBePlayed = true}
        if(nextLevelCanBePlayed){
            canBePlayedArray[indexPath.row] = true
            cell!.subLevelBack.backgroundColor = blueColor
        }else{
            canBePlayedArray[indexPath.row] = false
            cell!.subLevelBack.backgroundColor = redColor
        }
        cell!.subLevelLabel.text = "Stage \(indexPath.row+1)"
        let data = db.query("SELECT * FROM LEVEL_DETAIL WHERE MAIN_LEVEL =\(MainLevelNr!) AND SUB_LEVEL = \(indexPath.row+1)")[0]
        var levelScore: Int = 0
        if let columnValue = data["COMPLETED"]{
            levelScore = columnValue.asInt()
            if (levelScore > 0){nextLevelCanBePlayed = true}else{nextLevelCanBePlayed = false}
            cell!.subLevelScore.image = UIImage(named: "\(levelScore)Stars.png")
        }
        return cell!
    }

      

My problem is when the view is loaded for the first time, it is subLevelBack.backgroundColor

set correctly, i.e. the color of the subvector is correct.

Although when I start scrolling it becomes a bit of a mess with different cells having the wrong background colors and I don't know how to solve this problem. I don't have the same problem with the image displayed in the UIImageView file.

Hopefully someone will point me in the right direction. Regards, Sander

+3


source to share


1 answer


if (indexPath.row == 0){nextLevelCanBePlayed = true}
if(nextLevelCanBePlayed){ ...

      

The above code seems to be wrong. You are trying to keep track of how many levels have been rendered, but every time the first cell gets re-rendered, it nextLevelCanBePlayed

will be reset. Maybe try simple if(nextLevelCanBePlayed || indexPath.row == 0){

.



Another thing is that you are making the assumption that the table view will re-render its cells near the viewport in order, but is not documented anywhere and is most likely not reliably correct, and is especially likely if you're scrolling backwards. So when the table view rebuilds cell 4 ( nextLevelCanBePlayed

calculates and sets to false) and possibly returns to cell 3 onwards, and even if 3 is actually rendered, it nextLevelCanBePlayed

will be false and 3 will display incorrectly. Then it starts to feel like your colors start to change randomly as you scroll up and down.

My suggestion is to use your methods correctly UITableViewDataSource

and use them to work with db and return appropriate data objects that represent the state and data of your cells.

+1


source







All Articles