0

私は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]];
}
4

1 に答える 1

3

のインデックスはNSIndexSet順番に並んでいます。最高を見つけるためのループは必要ありません。

-selectRowIndexes:byExtendingSelection:特定の行を選択したい場合は、確立したい新しい選択で呼び出すだけです。たとえば、[m_tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:highestSelectedRowIndex] byExtendingSelection:NO]. それがどのオブジェクトであるかを知る必要はありません。

それでもオブジェクトを知りたい場合は、アレイコントローラーを取得してそれarrangedObjectsに適用-objectAtIndex:する必要があります。

于 2012-04-18T12:42:10.380 に答える