问题
I have a NSTableVIew for multi-selection purposes with two columns, the first one with a NSButtonCell as checkbox and the other one as a title.
The idea is to check the items to be added afterwords to an array.
The problem is that the checkboxes don't change its state when I click them. I've tried to attach an IBAction but the sender to de action is the TableView but not the checkbox
Any ideas (or link) about how to achieve this kind of functionality?
回答1:
Going on the assumption you're using a NSTableViewDataSource
you need to implement three methods:
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
When the checkbox is clicked the first method is called. If aTableColumn has your checkboxes you would save the new state, which is [anObject boolValue]
.
When the table needs to draw a row, it calls the second method. When the table column is your checkbox column, return the state that you saved in the first method.
The last method tells the table view how many rows there will be.
See the table data source documentation for more details, but I've pretty much summarized it here.
来源:https://stackoverflow.com/questions/4313490/nsbuttoncell-as-checkbox-in-a-nstableview-dont-get-selected