开发者

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 f开发者_StackOverflowilling 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]; 


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 !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜