一、NSTableView增加勾选框,下拉框
在NStableView中每一列都相对应着一个类型,如 TextCell类型,简单文本框类型,如需加入按钮下拉框等功能可相应修改其类型,加入类型相应边位***Cell类型,如NSButton变为NSButtonCell
具体操作:
第一步:新建一个NStableView
第二步:拖拉替换相应的列表
第三步:结果
第四步:代码设置Button内容;设置便可实现勾选框的加载了
if ([identifier isEqualToString:@"state"])
{//勾选框设置内容
NSButtonCell *box=cell;
box.state=[[value objectForKey:@"state"] integerValue]; }
运行结果:
以上便是NSTableView加载勾选框的过程,下拉框加载过程一致。
二、NSTableView行表拖拽换行
在实际的开发过程中,我们经常使用NStableView,拖拽进行行排序,简单方便。拖拽行表实现换行功能
具体操作:
第一步:首先定义NStableview为可拖拽类型
//MyTableCellViewDataType自定义类型
[tableViewLoop registerForDraggedTypes:[NSArray arrayWithObject:MyTableCellViewDataType] ];
第二步:设置响应,建立属性面板
-(BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexestoPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
NSData *zNSIndexSetData = [NSKeyedArchiverarchivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArrayarrayWithObject:MyTableCellViewDataType] owner:self];
[pboard setData:zNSIndexSetDataforType:MyTableCellViewDataType];
return YES;
}
第三步:响应处理,替换数据
-(NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op
{
// Add code here to validate the drop
//NSLog(@"validate Drop");
return NSDragOperationEvery;
}
-(BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{NSMutableDictionary*dic;
if(aTableView==tableViewLoop)
{
dic=dictionaryLoop;
}
else if(aTableView==tableViewlist)
{
dic=dictionaryList;
}
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboarddataForType:MyTableCellViewDataType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiverunarchiveObjectWithData:rowData];
NSInteger dragRow = [rowIndexesfirstIndex];
NSString *key1=[NSString stringWithFormat:@"%d",(int)dragRow];
NSString *value1=[dic valueForKey:key1];
NSString *key2=[NSStringstringWithFormat:@"%d",(int)row];
NSString *value2=[dic valueForKey:key2];
[ dic setObject:value2 forKey:key1];
[ dic setObject:value1 forKey:key2];
if(aTableView==tableViewLoop)
{
dictionaryLoop=dic;
}
else if(aTableView==tableViewlist)
{
dictionaryList=dic;
}
[self save];
[aTableView reloadData];
return YES;
}