简体   繁体   中英

Custom selected Icon for NSTableView Cell

I have created a view based NSTableView with custom cells. Each cell has an image(from net) in it. On selecting am showing a custom selection indicator(something like a check mark).

When any cell gets clicked it will modify that cell's property called isClicked to YES in tableViewSelectionDidChange delegate. Once done I will make a call to reload table data. While reloading a check is made to see if that cell was selected and if selected the selection indicator image will be displayed.

Until now cell image were from local, but now am using cell image from net. And this is loaded using dispatch async queue Since table gets reloaded all of these images gets downloaded again, and it sort of flickers on the screen. How can this be avoided ?

My solution with ugly code, but worked:

- (void)tableViewSelectionDidChange:(NSNotification *)notification{    

    NSTableView *tableView = [notification object];

    NSInteger row = [tableView selectedRow];

    [self markSelectedCellForTable:tableView withRow:row];

    if ([_foldersList count]) {
        _currentFolder = [_foldersList objectAtIndex:row];

        [tableView scrollRowToVisible:row];
    }else{
        _currentFolder = nil;
    }

    NSLog(@"Folder %@",(_currentFolder)?[_currentFolder title]:@" not selected");
}

- (void)markSelectedCellForTable:(NSTableView *)tableView withRow:(NSInteger)row{

//ui
@try {
        NSTableRowView *rowView;
        NSTableCellView *cellView;
        for (int i=0; i<=[_foldersList count]; i++){

            rowView = [tableView rowViewAtRow:i
                              makeIfNecessary:NO];

            cellView = [[rowView subviews] objectAtIndex:0];

            [[cellView imageView] setHidden:(i!=row)];
        }

    }
    @catch (NSException *exception) {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM