nlampi / SwiftGridView

Swift based data grid view.
MIT License
58 stars 15 forks source link

problem setting background color of specific cells #49

Closed SogoGolf closed 5 years ago

SogoGolf commented 5 years ago

we'd like to be able to set the background color of just specific cells in the grid. for example: row index 4, column index 3 = .green (background color of cell) row index 4, column index 16 = .red (background color of cell)

At the moment either we've not set the property correctly, or there perhaps there is a bug with cell recycling.

for example, in the data source cellAtIndexPath method there is this attempt:

    func dataGridView(_ dataGridView: SwiftGridView, cellAtIndexPath indexPath: IndexPath) -> SwiftGridCell {
        let cell = dataGridView.dequeueReusableCellWithReuseIdentifier(PrettyRowCell.reuseIdentifier(), forIndexPath: indexPath) as! PrettyRowCell

    cell.mainLabel.font = UIFont(name: "Montserrat-Regular", size: 16)

    if is18Holes {
        let row = self.dataGridRows18![indexPath.sgRow]

        switch indexPath.sgColumn {
        case 0:
            cell.mainLabel.text = row.name
        case 1:
            cell.mainLabel.text = String(row.hole1)
                               cell.backgroundView!.backgroundColor = .yellow  <=====
        case 2:
            cell.mainLabel.text = String(row.hole2)
                        ...more case statements....

The grid in question has 22 columns.

If you scroll all the way to the right, the last column in the grid also has a yellow background.

If you scroll back to the left, the originally highlighted column is no longer highlighted, and instead a different column is highlighted. It seems like the cell recycling isn't working correctly for this use case however we may also not have set things up correctly in code.

How should we set things up so that we can achieve this ? eg. Should something be done with the PrettyRowCell ?

Appreciate your help with this.

nlampi commented 5 years ago

Hi @SogoGolf, Sorry I was out for a bit, but this looks to be a general dequeueing issue. If you are setting the cell color in the dataSource, you'll need to also reset the color in the cell for reuse. e.g.

prepareForReuse() {
    self.backgroundView!.backgroundColor = .white
}

Let me know if this isn't the problem you're running into.

SogoGolf commented 5 years ago

Thanks @nlampi I'll take a look at and let you know