CheckBox in tableview

对着背影说爱祢 提交于 2019-12-13 11:34:21

问题


I am facing trouble in putting check boxes into a UITableView. I am posting a part 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 the checkboxes inside the UITableView. But the problem is that I can't uncheck the buttons. Is there anything more to do. I am new to cocoa programming.


回答1:


You're missing a couple of important pieces. You need to update your model (data stcuture) in response to the tableValue:setObjectValue:forTableColumn:row: message, so that you can correctly return the new value from tableView:objectValueForTableColumn:row: method.

Here are some table data source methods assuming you have a 'myRows' array filled with objects with a 'booleanAttribute' property.

- (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]];
}

You should also setup your table cell in interface builder. You can drag a button cell configured like a standard check box directly onto one of your table columns.




回答2:


I'm not sure why you're creating the cell in code. You can just drag the cell onto the table column in Interface Builder.

Also, setObjectValue: is where you respond to the change in the cell's state. The user has already changed the cell's state to off; then you send setNextState and change it back. That's why the cell doesn't appear to uncheck: you keep re-checking it.

What you need to do is not touch the cell at all, but set the object value (which, for this column, will probably be a Boolean NSNumber containing either YES or NO) as the new value of the appropriate property in your model.

Also, of course, make sure the column is set as editable.




回答3:


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



来源:https://stackoverflow.com/questions/1021346/checkbox-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!