私はInterfaceBuilderのNSArrayControllerを使用して、NSTableViewに表示されるオブジェクトを管理しています。1つまたは複数の行を選択すると、次のコードが呼び出されてオブジェクトが削除され、選択が更新されます。
NSIndexSet* selectedRowIndices = [m_tableView selectedRowIndexes];
if (!selectedRowIndices || selectedRowIndices.count < 1) {
return;
}
[self removeObjectsAtIndices:selectedRowIndices];
// -------------------------------------------------------
// SELECT ROW BELOW OR EQUAL TO THE LAST DELETED ROW.
// -------------------------------------------------------
// Retrieve the highest selection index (lowest row).
NSInteger highestSelectedRowIndex = NSNotFound;
for (NSUInteger index = selectedRowIndices.firstIndex; index < selectedRowIndices.count; ++index) {
if (index > highestSelectedRowIndex) {
highestSelectedRowIndex = index;
}
}
if (highestSelectedRowIndex != NSNotFound && highestSelectedRowIndex < [m_tableView numberOfRows]) {
// 1) Get the selected object for the highest selected index.
// TODO: Retrieve the object from m_tableView or m_arrayController somehow!?!
// 2) Update the table view selection.
//[self updateTableViewWithSelectedObjects:...];
}
ただし、前の選択の最高のインデックスと一致するオブジェクトを特定する方法がわかりません。
なんで?新しい選択を最後の選択の下の行に移動したいと思います。
注意:上記のコードにはいくつかのエラーが含まれています!
これが私が最終的に得たものです-説明してくれたThomasに感謝します。
NSUInteger nextSelectedRowIndex = selectedRowIndices.firstIndex;
if (nextSelectedRowIndex != NSNotFound) {
if (nextSelectedRowIndex >= m_tableView.numberOfRows) {
nextSelectedRowIndex = m_tableView.numberOfRows - 1;
}
id nextSelection = [[m_arrayController arrangedObjects] objectAtIndex:nextSelectedRowIndex];
[self updateTableViewWithSelectedObjects:nextSelection]];
}