iPhone开发应用中NSTableView相关操作是本文要介绍的内容,主要是来学习NSTableView的使用方法,如何使NSTableView同时支持拖拽替换和拖拽插入。
当你的NSTableView做为一个拖拽目标时,你可能希望同时支持拖拽替换当前项目,或者拖拽后在当前位置插入新的项目。你需要使用NSTableView的 -setDropRow:dropOperation:方法。本文介绍如何通过代码实现NSTableView的这种拖拽功能。
代码如下所示:
- (NSDragOperation) tableView: (NSTableView *) view
validateDrop: (id ) info
proposedRow: (int) row
proposedDropOperation: (NSTableViewDropOperation) op
{
[view setDropRow: row
dropOperation: op];
NSDragOperation dragOp = NSDragOperationCopy;
return (dragOp);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
同时,在acceptDrop方法里进行如下操作:
- (BOOL) tableView: (NSTableView *) view
acceptDrop: (id ) info
row: (int) row
dropOperation: (NSTableViewDropOperation) op
{
if (op == NSTableViewDropOn) {
// 替换
} else if (op == NSTableViewDropAbove) {
// 插入
} else {
NSLog (@"unexpected operation (%d) in %s",
op, __FUNCTION__);
}
return (YES);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
在NSTableView选择项改变时获取通知
代码如下所示:
- (void) tableViewSelectionDidChange: (NSNotification *) notification
{
int row;
row = [tableView selectedRow];
if (row == -1) {
//do stuff for the no-rows-selected case
}
else {
// do stuff for the selected row
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
这段代码需要放在NSTableView的delegate里。如果没有delegate,可以将自身设置为delegate。
小结:iPhone开发应用中NSTableView相关操作的内容介绍完了,希望通过本文的学习能对你有所帮助!