iPhone開(kāi)發(fā)應(yīng)用中NSTableView相關(guān)操作
iPhone開(kāi)發(fā)應(yīng)用中NSTableView相關(guān)操作是本文要介紹的內(nèi)容,主要是來(lái)學(xué)習(xí)NSTableView的使用方法,如何使NSTableView同時(shí)支持拖拽替換和拖拽插入。
當(dāng)你的NSTableView做為一個(gè)拖拽目標(biāo)時(shí),你可能希望同時(shí)支持拖拽替換當(dāng)前項(xiàng)目,或者拖拽后在當(dāng)前位置插入新的項(xiàng)目。你需要使用NSTableView的 -setDropRow:dropOperation:方法。本文介紹如何通過(guò)代碼實(shí)現(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);
- }
同時(shí),在acceptDrop方法里進(jìn)行如下操作:
- - (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選擇項(xiàng)改變時(shí)獲取通知
代碼如下所示:
- - (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里。如果沒(méi)有delegate,可以將自身設(shè)置為delegate。
小結(jié):iPhone開(kāi)發(fā)應(yīng)用中NSTableView相關(guān)操作的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!