【问题标题】:Remove NSTableCellView删除 NSTableCellView
【发布时间】:2013-03-25 21:11:24
【问题描述】:

如何从我的 NSTableView 中手动删除 NSTableCellView(或其子类)?

当我清空包含信息的数组时,将 DB 排队以获取新信息,然后重新加载表数据,它只会使用与新数组列表中的索引值相似的索引值更新单元格。

例如

我的数组中首先有 8 个对象,它们显示正常。当我更新我的数组并且现在只有 3 个时,表中的前三个单元格得到更新,而最后 5 个仍然是旧的。旧的不可选择,但仅可见。而且我怀疑我错过了一些重绘/dealloc..

我在 TableView 上尝试了 setNeedsDisplay,但还没有成功。

我正在使用 ARC、Xcode 4.6.1 和 OS X 10.8.3

示例代码

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

【问题讨论】:

  • 同意@matt - 你能发布 numberOfRowsInSection: 和 cellForRowAtIndexPath: 数据源方法吗?
  • 基本配方是这样的:你有一个支持表格的数组。 numberOfRows 回答了数组长度,任何带有 indexPath 的签名都会取消引用 indexPath.row 处的数组。更改数组并(在最简单的情况下)调用 [tableView reloadData];
  • 我终于发现了问题(在一点一点注释掉几乎所有内容之后)。我的 cellView 类中有一个空的 drawRect,它搞砸了重绘。

标签: objective-c cocoa nstableview nstablecellview


【解决方案1】:

您必须将 NSTableView 告诉reloadData(听起来您正在这样做)。当你这样做时,数据源的工作就是知道现在只有 3 行并在numberOfRowsInTableView: 中给出答案(听起来你没有这样做)。您没有显示任何代码(为什么不显示?)所以当然所有这些都是猜测。

【讨论】:

  • 我这样做了,它会返回正确数量的行,然后绘制它们。问题是基于不再存在的信息的视图仍然在屏幕上。我现在不在家,但我可以在今天晚些时候发布一些示例。
【解决方案2】:

使用

[tableView removeRowsAtIndexSet:indexSet withAnimation:YES];

【讨论】:

  • 这不只是删除行吗?因为我有一个委托方法通知我行视图已被删除。 (tableView:didRemoveRowView:forRow:)
【解决方案3】:

您可以使用此示例(清除/刷新/重绘)您的 cellView:

  1. 创建与原始框架相同的新单元格。
  2. 使用与原始相同的框架重绘它。
  3. 将原始对象放入其中。

在这个例子中,“mainTable”是你表的 NSTableView*。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多