vendredi 20 juillet 2018

Cocoa tableview checkbox state lost on scroll

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.state = NSControl.StateValue(rawValue: 0)
                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
            }
        }
    }

I have search on google and stackoverflow and I find out the way to solve this is method:

func tableView(_ tableView: NSTableView, rowViewForRow row:Int) -> NSTableRowView? {

}

Although I don't know to implement this method.




Aucun commentaire:

Enregistrer un commentaire