CheckBox as a table

I ran into the problem of placing checkboxes in a UITableView. I am posting some of my code here.

- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSButtonCell *cell=[[NSButtonCell alloc] init];
    NSString *strDisplayPlaylistName;
    strDisplayPlaylistName=[playListNameArray objectAtIndex:row];
    [cell setTitle:strDisplayPlaylistName];
    [cell setAllowsMixedState:YES];
    [cell setButtonType:NSSwitchButton];
    return cell; 
}


- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {          
    NSCell *aCell = [aTableColumn dataCellForRow:rowIndex];
    [aCell setNextState];
    //NSCell *aCell=[aAddedCells objectAtIndex:rowIndex];
    //[aCell setNextState];
}

I got checkboxes inside a UITableView. But the problem is that I can not uncheck the buttons. There is still something to do. I am new to cocoa programming.

+3
source share
3 answers

You are missing a few important parts. You need to update your model (data stcuture) in response to the tableValue: setObjectValue: forTableColumn: row: message command so that you can correctly return the new value from tableView: objectValueForTableColumn: row: method.

, , myRows, 'booleanAttribute'.

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return [myRows count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    BOOL value = [[myRows objectAtIndex:row] booleanAttribute];
    return [NSNumber numberWithInteger:(value ? NSOnState : NSOffState)];
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)value forTableColumn:(NSTableColumn *)column row:(NSInteger)row {          
    [[myRows objectAtIndex:row] setBooleanAttribute:[value booleanValue]];
}

. , , .

+4

, . Interface Builder.

, setObjectValue: . ; setNextState . , , : .

, , ( , , NSNumber, YES, NO), .

, , , .

+2

If you have the NSTableView content mode set to "Cell Based", it will be "View Based" when you move the checkbox.

+2
source

Source: https://habr.com/ru/post/1710900/


All Articles