简体   繁体   中英

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

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.

采用

[tableView removeRowsAtIndexSet:indexSet withAnimation:YES];

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

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