1

我已经为我的学校创建了一个表格视图作为应用程序的一部分,它允许学生输入他们的家庭作业,然后检查它并让它消失,就像提醒应用程序一样。我遇到的问题是,当我检查一行以使其消失时,我从表中删除了该行,并HomeworkItem从包含所有HomeworkItems 的数组中删除了它的数据;但是,下次我尝试添加一行时(通过单击末尾的缓冲区行),添加的新行与我刚刚删除的行相同,具有相同的HomeworkItem信息,即使该行HomeworkItem已从数组中删除。我记得读过 NSableViews 尝试重用行以提高内存效率,但我不明白为什么该行会挂在旧信息上。

这是我的自定义 TableViewCell 代码:

作业表ViewCell.h:

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"
#import "ClassViewController.h"

@class ClassViewController;
@class AppDelegate;

@interface HomeworkTableCellView : NSTableCellView

@property (weak) IBOutlet NSTextField *assignmentName;
@property (weak) IBOutlet NSDatePicker *dueDatePicker;
@property BOOL isCompleted;
@property NSInteger rowNumber;
@property (weak) AppDelegate *ad;
@property NSTableView *superTable;
@property ClassViewController *superCVC;

- (void)setSuperRow:(id)sender;
- (IBAction)completed:(id)sender;
- (IBAction)dateChanged:(id)sender;

@end

作业表ViewCell.m:

#import "HomeworkTableCellView.h"

@implementation HomeworkTableCellView

- (id)initWithCoder:(NSCoder *)aDecoder{
//    NSLog(@"I");//TEST
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.ad = [[NSApplication sharedApplication]delegate];
        self.superTable = self.ad.classViewController.homeworkTable;
        self.superCVC = self.ad.classViewController;
        self.isCompleted = NO;
    }
    return self;
}

- (void)setSuperRow:(id)sender{
    [self.superCVC updateRow:sender];
}
- (IBAction)completed:(id)sender{
    self.isCompleted = !self.isCompleted;
    [self.superCVC changeStateOfHomeworkItem:self];
    [self.superCVC updateRow:sender];
}
- (IBAction)dateChanged:(id)sender{
    [self.superCVC updateRow:sender];
    [self.superCVC editDateOfHomeworkItem];
}
@end

这里是出现问题的视频链接:http: //youtu.be/Ot8w5oz8L-E

控制台中的文本表示删除对象后作业数组中的内容,因此很明显已删除的对象不再存在。

编辑:这是我使用 uasi 的建议提出的:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    HomeworkTableCellView *HTCV = [self.homeworkTable makeViewWithIdentifier:tableColumn.identifier owner:self];//maybe owner:nil
    [HTCV.assignmentName setStringValue: @""];
    [HTCV.dueDatePicker setDateValue: [[NSDate alloc]initWithString:@"1-1-1 0:0:0 +0000"]];//false date for testing
    HTCV.isCompleted = NO;
    [HTCV.checkBox setState:NSOffState];

    [self updateTableView];

    return HTCV;
}

- (void)updateTableView{
    for (int i = 0; i < self.ad.currentSubject.homework.count; i++) {
        HomeworkTableCellView *HTCV = [self.homeworkTable viewAtColumn:0 row:i makeIfNecessary:NO];
        HomeworkItem *HI = [self.ad.currentSubject.homework objectAtIndex:i];
        [HTCV.dueDatePicker setDateValue: HI.dueDate];
        [HTCV.assignmentName setStringValue: HI.details];
    }
}
4

2 回答 2

4

您可能不用于-tableView:objectValueForTableColumn:row:制作表格单元格视图。虽然该文档声称“此方法是强制性的”,但对该方法的评论<AppKit/NSTableView.h>说“基于单元格的”TableView 需要此方法,而“基于视图”的 TableView 是可选的

相反,您可以使用NSTableViewDelegate's-tableView:viewForTableColumn:row:方法:

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row
{
  // Instantiate a view object from XIB or reuse one in the cache.
  NSString *identifier = @"YOUR_CELL_VIEW_IDENTIFIER";
  HomeworkTableCellView *HTCV = [tableView makeViewWithIdentifier:identifier owner:nil];

#if 0
  // If you have not put a HomeworkTableCellView object in a XIB with identifier,
  // you'll need to create an instance programatically like this;
  // It seems not to be the case, though.
  if (HTCV == nil) {
    HTCV = [[HomeworkTableCellView alloc] initWithFrame:NSZeroRect];
    HTCV.identifier = identifier;
  }
#endif

  // Do initialization anything specific to a row here,
  // because the view's -init{WithCoder:,WithFrame:} method
  // is not always called for each row due to the cache mechanism.
  HomeworkItem *HI = [self.homeworkArray objectAtIndex:row];
  [HTCV.dueDatePicker setDateValue: HI.dueDate];
  // ...

  return HTCV;
}
于 2013-07-02T08:40:09.327 回答
1

我知道这个问题有点老了,但我只是把它放在那里给像我自己这样正在寻找解决方案的人。

if ([self.tableView respondsToSelector:@selector(usesStaticContents)])
  //Method currenly only available on OS X 10.10 or later
  {
    //This is used to prevent table view rows from being reused
    /*From:https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKitOlderNotes/
      Normally, a table view will only keep around a subset of the total number of rows potentially available (in general, this is limited to the visible region, plus some overdrawn allowance for responsive scrolling). A static table view keeps all views added to the table around when usesStaticContents=YES
      */
      //Also check out: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TableView/TableViewOverview/TableViewOverview.html

     self.tableView.usesStaticContents = YES;

   }

注意:通过使用此解决方案,您的应用程序可能会表现出更高的内存利用率(原因很明显)。但是,在我的测试中,差别不大,您的里程可能会有所不同。

于 2015-10-27T05:27:21.047 回答