Group Group Group Group Group Group Group Group Group

How to saved checkbox state in NSTableview

I have a viewbase tableview with checkboxes. I create custom class for nstableviewcell to manipulate the checkbox for every rows.

The problem is when I checked mutiple checkboxes in tableview and scroll down. Then I scroll back, the checkbox state disappeared although the selected data was in the array.

Here is my tableview

else if tableView.tag == 2{
    if (tableColumn?.identifier)!.rawValue == "AlbumColumn" .   {
        if let cell: MyCustomViewCell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AlbumColumn"), owner: self) as? MyCustomViewCell
        {
      
            cell.AlbumCbx.title = albums[row]!
            cell.onClickAlbumCbx = { sender in
                if !selected_album.isEmpty{
                    if cell.AlbumCbx.state.rawValue == 1{
                        selected_album.append(albums[row]!)

                    } else {
                        selected_album = selected_album.filter({ $0 != albums[row]! })
                    }
                }
            }

            return cell
        }
    }
}

The checkbox state moved to another checkbox after I scrolling up and down fast. Are there any way to prevent this?

Hi @crystark,
from the text of the issue it seems to be a problem where the cell is reused when you scroll fast and the checkboxes are associated with data from another cell record.

the line,

let cell = tableView.makeView ...

This retrieves the cell from a pool of reusable cells and a cells that is no longer on screen might have been configured with some data. However, because of the if statement, the cell is either not initialised or reset and retains the data from its previous state. In short a different state than what you have set or have in the data.

A good way to handle this is to set or remove the checkbox, i.e. handle both the conditions of an if…else statement. That way the data and the UI will be in sync and not have any crossovers.

cheers,

Jayant

1 Like

for more official description from Apple on how tableViews work, you can refer to this document

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TableView/TableViewOverview/TableViewOverview.html#//apple_ref/doc/uid/10000026i-CH2-SW1

and the last section would help understand the same in a flow diagram.

cheers,

Jayant

1 Like

Thank you for your guidance, I wrote like this and it worked like charms

cell.AlbumCbx.title = albums[row]!
cell.AlbumCbx.state = NSControl.StateValue(rawValue: 0)
            
            for s in selected_album{
                if cell.AlbumCbx.title == s
                {
                    print(true, cell.AlbumCbx.title)
                    cell.AlbumCbx.state = NSControl.StateValue(rawValue: 1)
                    
                }
            }
            
            cell.onClickAlbumCbx = { sender in
                if cell.AlbumCbx.state.rawValue == 1{
                    selected_album.append(albums[row]!)
                
                    
                } else {
                    selected_album = selected_album.filter({ $0 != albums[row]! })
                    
                }
            }
            return cell

This topic was automatically closed after 166 days. New replies are no longer allowed.