User1234
User1234

Reputation: 2412

How to Add Checkbox Cell to NSTableView with Cocoa Bindings?

I'm new in Cocoa. I have a table that stores Contacts. I'm not using NSTableView Protocol. I'm using Cocoa Bindings , and Mutable Array for storing in it. The question is , I want to be able to have checkbox on each row, and select contacts that I want, and be able to get selected rows. How can I do it? When I try to put Check box cell in column of Table, nothing works. Here my code how I'm filling my NSTableView for contacts. So what should be done to be able add checkboxes and handle them? Thanks.

  NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                       Name, @"Name",
                        Surname, @"Surname",
                         Education,@"Education",
                         nil];

    //controller for binding data to NSTableView
    [controllerContacts addObject:dict];
  //Table for viewing data.
    [viewTableOfContacts reloadData]; 

Upvotes: 2

Views: 2044

Answers (1)

Devarshi
Devarshi

Reputation: 16758

If you want to use checkbox at each row via bindings, you need to define a key in dict corresponding to your binding for check box.

In your case, it can be like this-

  NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                       Name, @"Name",
                        Surname, @"Surname",
                         Education,@"Education",
                         [NSNumber numberWithInt:0],@"selectedStudent",
                         nil];

Then bind that column for "selectedStudent" key.

Also if you want to retrieve only selected rows, you can use NSPredicate like this-

NSPredicate *selectedStudentPredicate = [NSPredicate predicateWithFormat:@"selectedStudent == 1"];
NSArray *selectedStudents = [controllerContacts filteredArrayUsingPredicate:selectedStudentPredicate];

Hope this helps !

Upvotes: 2

Related Questions