NSTableView中添加CheckBox

chensunwang 2017-10-16 04:03:52
如题 见下图效果 我在表中添加CheckBox之后 我选择CheckBox之后我要怎么知道选择的时哪一行

表格这样实现的

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSTableCellView *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
NSButton *cell1 = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
CompSameTimeDifSensorModel *comp = dataArray[row];
if ([tableColumn.identifier isEqualToString:@"Selected"]) {
[cell1 setTag:row];
[cell1 setTarget:self];
[cell1 setAction:@selector(cellClick:)];

[cell1 setState:0];
return cell1;
}
else if ([tableColumn.identifier isEqualToString:@"SensorID"]) {
cell.textField.stringValue = comp.SensorID;
}
else if ([tableColumn.identifier isEqualToString:@"MinRecordTime"]) {
NSString *timeStr = [NSString stringWithFormat:@"%f",comp.MinRecordTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeStr integerValue]/1000.0];
cell.textField.stringValue = [formatter stringFromDate:confromTimesp];
}
else if ([tableColumn.identifier isEqualToString:@"MaxRecordTime"]) {
NSString *timeStr = [NSString stringWithFormat:@"%f",comp.MaxRecordTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeStr integerValue]/1000.0];
cell.textField.stringValue = [formatter stringFromDate:confromTimesp];
}
return cell;
}


我给每个CheckBox添加了个方法,并赋值tag为行号,但是方法里面得不到CheckBox的tag且都为0,这是怎么回事

- (void)cellClick:(NSButton *)sender
{
if(sender != nil){
NSButton* cell = (NSButton *)[self.tableView selectedCell];
if([cell state] == 1)
[cell setState:0];
else {
[cell setState:1];
}
NSLog(@"cellClick = %ld",[[self.tableView selectedCell] tag]);
}
}



- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row

这个代理方法是选择当前行才会调用 如果选择当前行里面的CheckBox则不会调用

那该怎么实现呢,效果图及tableview结构如下

...全文
458 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧