d00dle
d00dle

Reputation: 1316

Remove NSTableCellView

How do I maunally remove a NSTableCellView (or a subclass of it) from my NSTableView?

When i empty my array holding information, queue DB for new information and then reload the tables data, it only updates the cell with index values similar to ones found in the new array list.

ex.

I first have 8 objects in my array and they display fine. When I update my array and now only have 3, the top three cells in the table gets updated, while the last 5 remains the old ones. The old one are not selectable but only visible. And I suspect i miss some redraw/dealloc..

I tried setNeedsDisplay on the TableView but with no luck yet.

Im using ARC, Xcode 4.6.1, and OS X 10.8.3

Sample Code

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {

     return [_array count];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

     [_tableView setNeedsDisplay];

     NSDictionary *d = [_array objectAtIndex:row];
     NSString *identifier = [tableColumn identifier];

     if ([identifier isEqualToString:@"MainCell"]) {

         CustomCellView *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];
         [cellView.textField setStringValue:d[@"from"]];
         [cellView.subject setStringValue:d[@"subject"]];
         [cellView.text setStringValue:d[@"text"]];
         [cellView.date setStringValue:d[@"date"]];

         return cellView;
     }

     return nil;
}

Upvotes: 0

Views: 636

Answers (3)

Daniel Valeriev
Daniel Valeriev

Reputation: 358

You can (clear/refresh/redraw) you cellView with this example:

  1. Create new cell with same frame as original.
  2. Redraw It with the same frame as original.
  3. Put the original object in it.

In this example the "mainTable" is NSTableView* of you table.

- (NSView *)tableView:(NSTableView *)aTableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    NSString* identifier = [tableColumn identifier];
    NSTableCellView* cellView = [[NSTableCellView new] initWithFrame:[[mainTable makeViewWithIdentifier:identifier owner:self] frame]];

    [cellView drawRect:[[mainTable makeViewWithIdentifier:identifier owner:self] frame]];
    cellView = [mainTable makeViewWithIdentifier:identifier owner:self];

    // do something with your cellView here ...

    return cellView;
}

Upvotes: 0

egor.zhdan
egor.zhdan

Reputation: 4595

Use

[tableView removeRowsAtIndexSet:indexSet withAnimation:YES];

Upvotes: 0

matt
matt

Reputation: 535159

You have to tell the NSTableView to reloadData (it sounds like you are doing that). When you do, it is the job of the data source to know that there are now only 3 rows and to give that answer in numberOfRowsInTableView: (it sounds like you are not doing that). You don't show any code (why not?) so of course all of that is a total guess.

Upvotes: 1

Related Questions