Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make reuse cells in NSTableView?

I have a NSTableView with 10 cells. They are all the same cells and I want to reuse them, however I can't seem to get the table view to do so.

Currently, each time a new cell is needed, the delegate cannot seem to find the previous cell with the same identifier and has to recreate it.

Below is my code, any suggestions on what I might be doing wrong?

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
    return 10;
}


- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    static NSString *cellIdentifier = @"Name";

    NSTableCellView *cellView = [tableView makeViewWithIdentifier:cellIdentifier owner:self];

    if (cellView == nil)
    {
        NSLog(@"A cell");
        NSRect cellFrame = [_scrollView.contentView bounds];
        cellFrame.size.height = 65;

        cellView = [[NSTableCellView alloc] initWithFrame:cellFrame];
        [cellView setIdentifier:cellIdentifier];
    }

    return cellView;
}
like image 586
David Avatar asked Oct 28 '12 07:10

David


1 Answers

I did some testing and noticed that the table will continually create new cells until the table cannot display all cells at the same time.

At this point, the table will reuse the cells that are not visible.

like image 152
David Avatar answered Sep 28 '22 15:09

David