문제

내가 사용하는 NSOutlineView NSTreeController 하고 구현하는 내 자신의 datasource.는 가장 좋은 방법은 무엇입 항목을 선택하려면?NSOutlineView 이미 지원 expandItem:collapseItem:.나는 누락에 편리한 방법은 다음과 같`selectItem:.나는 어떻게 그것을 사용하여 프로그래밍 방식으로?

감사합니다.

도움이 되었습니까?

해결책

무언가를 찾을 수 없을 때 슈퍼 클래스를 보는 것을 잊지 마십시오. 이 경우 필요한 메소드 중 하나는 NStableView에서 나온 것입니다. NSoutlineView의 즉시 슈퍼 클래스입니다.

해결책은해야합니다 사용하는 항목의 행 색상을 가져옵니다 rowForItem:, 그리고 -1이 아닌 경우 (아이템이 보이지 않거나 찾을 수 없음), 그것으로 설정된 색인을 만듭니다 [NSIndexSet indexSetWithIndex:] 그리고 그 지수를 설정합니다 그만큼 selectRowIndexes:byExtendingSelection: 방법.

다른 팁

여기에 내가 마침내 끝난 방법은 다음과 같습니다. 제안과 수정은 항상 환영합니다.

@implementation NSOutlineView (Additions)

- (void)expandParentsOfItem:(id)item {
    while (item != nil) {
        id parent = [self parentForItem: item];
        if (![self isExpandable: parent])
            break;
        if (![self isItemExpanded: parent])
            [self expandItem: parent];
        item = parent;
    }
}

- (void)selectItem:(id)item {
    NSInteger itemIndex = [self rowForItem:item];
    if (itemIndex < 0) {
        [self expandParentsOfItem: item];
        itemIndex = [self rowForItem:item];
        if (itemIndex < 0)
            return;
    }

    [self selectRowIndexes: [NSIndexSet indexSetWithIndex: itemIndex] byExtendingSelection: NO];
}
@end

아니요, A가 없습니다 selectItem: 방법이지만 an이 있습니다 rowForItem: 방법. Peter의 사용에 대한 조언과 결합하면 selectRowIndexes:byExtendingSelection: 위의 경우 필요한 모든 정보가 있어야합니다.

실제로 항목을 선택하는 방법을 원한다면 전화하는 것이 좋습니다. setSelectedItem: 일관성을 위해, 당신은 이와 같은 것을 카테고리로 쓸 수 있습니다. NSOutlineView

- (void)setSelectedItem:(id)item {
    NSInteger itemIndex = [self rowForItem:item];
    if (itemIndex < 0) {
        // You need to decide what happens if the item doesn't exist
        return;
    }

    [self selectRowIndexes:[NSIndexSet indexSetWithIndex:itemIndex] byExtendingSelection:NO];
}

이 코드가 실제로 작동하는지 모르겠습니다. 개념을 설명하기 위해 방금 마무리했습니다.

다음은 pxsourcelist에서 항목을 프로그래밍 방식으로 선택하는 데 사용한 코드 스 니펫입니다.

Sourcernist는 일반 PXSoucelist 객체이며 첫 번째 개요 그룹에서 두 번째 항목을 선택하고 싶었습니다.

    NSInteger itemRow = [sourceList rowForItem:[[(SourceListItem *)[sourceListItems objectAtIndex:0] children] objectAtIndex:1]];
    [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:itemRow] byExtendingSelection:YES];

아직 모르는 경우 PXSOURCELIST는 iTunes/Mail Style 개요를 찾는 경우 NSoutlineView의 훌륭한 대체품입니다. 여기에서 픽업하십시오.pxsourcelist

이것은 오래된 질문이지만,상황이 여전히 동일합니다.가 요청을 신속한 버전이 여기 나의 걸립니다.지 않았을 찾을 받아들이 대답을 작품으로 나를 위해 생각해야 합와 직접 상호 작용 데이터 소스가 아닌 클래스를 확장하 NSOutlineView.에게 완벽 윤곽을 찾을 수 없 행하지 않는 한 그들이 확장되는 이유 그것은 간단하게 사용하 dataSource.내 경우에는 것을 발견 있을 확장하는 부모의 항목의 역순으로,시작할 수 있도록 최상위 레벨에서 작동하는 방법으로 실제 항목하려는 확장하고 있습니다.나는 같은 느낌을 내장해야에서,그러나지 않는 한 나는 뭔가를 놓친다-그것은 없습니다.

이 예제에서 FileItem 나의 데이터 원본을 수집 항목 클래스입니다.그것은 포함하는 시설"parent"할 필요가 유효한 경우에는 계층 부분에 표시됩니다.

func selectItem(_ item: FileItem, byExtendingSelection: Bool = false) {
    guard let outlineView = outlineView else { return }

    var itemIndex: Int = outlineView.row(forItem: item)

    if itemIndex < 0 {
        var parent: FileItem? = item

        var parents = [FileItem?]()
        while parent != nil {
            parents.append(parent)
            parent = parent?.parent
        }

        let reversedTree = parents.compactMap({$0}).reversed()

        for level in reversedTree {
            outlineView.expandItem(level, expandChildren: false)
        }

        itemIndex = outlineView.row(forItem: item)
        if itemIndex < 0 {
            print("Didn't find", item)
            return
        }
    }

    print("Expanding row", itemIndex)

    outlineView.selectRowIndexes(IndexSet(integer: itemIndex), byExtendingSelection: byExtendingSelection)
    outlineView.scrollRowToVisible(itemIndex)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top