iPhone開發(fā)應用中NSTableView相關操作
iPhone開發(fā)應用中NSTableView相關操作是本文要介紹的內(nèi)容,主要是來學習NSTableView的使用方法,如何使NSTableView同時支持拖拽替換和拖拽插入。
當你的NSTableView做為一個拖拽目標時,你可能希望同時支持拖拽替換當前項目,或者拖拽后在當前位置插入新的項目。你需要使用NSTableView的 -setDropRow:dropOperation:方法。本文介紹如何通過代碼實現(xiàn)NSTableView的這種拖拽功能。
代碼如下所示:
- - (NSDragOperation) tableView: (NSTableView *) view
- validateDrop: (id ) info
- proposedRow: (int) row
- proposedDropOperation: (NSTableViewDropOperation) op
- {
- [view setDropRow: row
- dropOperation: op];
- NSDragOperation dragOp = NSDragOperationCopy;
- return (dragOp);
- }
同時,在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);
- }
在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
- }
- }
這段代碼需要放在NSTableView的delegate里。如果沒有delegate,可以將自身設置為delegate。
小結(jié):iPhone開發(fā)應用中NSTableView相關操作的內(nèi)容介紹完了,希望通過本文的學習能對你有所幫助!