0

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;
}
3
  • agree with @matt - can you post the numberOfRowsInSection: and cellForRowAtIndexPath: datasource methods?
    – danh
    Mar 25, 2013 at 22:40
  • The basic recipe is this: you have an array backing the table. numberOfRows answers that array length, anything with indexPath in it's signature dereferences the array at indexPath.row. Change the array and (in the simplest case) call [tableView reloadData];
    – danh
    Mar 25, 2013 at 22:42
  • 1
    I found the problem at last (after commenting almost everything out bit by bit). I had an empty drawRect in my cellView class that messed the redraw up.
    – d00dle
    Mar 27, 2013 at 16:26

3 Answers 3

1

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.

1
  • i do that and it returns the right amount of rows and then draws them. The problem is the views wich is based on information that does not exist anymore, are still on-screen. Im not home right now, but i can post some examples later today.
    – d00dle
    Mar 26, 2013 at 9:00
0

Use

[tableView removeRowsAtIndexSet:indexSet withAnimation:YES];
1
  • Wont this just remove the rows? Because I have a delegate method that informs me that the rowview has been removed. (tableView:didRemoveRowView:forRow:)
    – d00dle
    Mar 26, 2013 at 13:44
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.