删除NSTableView项时出现奇怪的行为

时间:2014-04-09 12:49:01

标签: cocoa nstableview

我有一个没有绑定的基本NSTableView(基于视图)和一个包含几个控件的tableCellView(水平滑块和NSStepper)。我使用滑块和步进器来调整与该行关联的特定项目的值。除非我删除了一个项目,否则所有工作都按预期工作。

例如,如果我删除一个项目,然后尝试选择第2行中的滑块或步进器,它似乎也在第3行中选择相应的控件。我将每个控件连接到同一个Action,如下所示

- (IBAction)percentDidChange:(id)sender
{
    NSInteger row = [_allocationTable rowForView:sender];
    NSLog(@"The row is %ld", (long)row);
}

当我尝试调整第2行中的控件时,log语句将生成以下内容

The row is 2
The row is 3
The row is -1

如果我退出此视图,然后按预期返回所有工作。

我使用以下操作删除项目

- (IBAction)removeSector:(id)sender
{
    NSInteger row = [self.allocationTable rowForView:sender];

    Allocation *allocation = [self.dataSource objectAtIndex:row];

    [self.myManagedContext deleteObject:allocation];
    NSError *anyError = nil;
    BOOL success = [self.myManagedContext save:&anyError];
    if (!success) {
        DLog(@"Error:%@", anyError);
    }

    //Update all objects
    [self renumberViewPositions];
    [self updateDataSource];
    [_allocationTable reloadData];
    [self percentCenterTextField];
    [[[self hostView] hostedGraph] reloadData];
}

我的Tableview数据源方法是

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row
{
    //Setup Number Formatter
    SBIPercentFormatter *percentFormat = [[SBIPercentFormatter alloc] init];

    SBIAllocationCellView *result = [tableView makeViewWithIdentifier:@"MyView" owner:self];

    NSNumber *percentValue = [[self.dataSource objectAtIndex:row] valueForKey:@"sectorPercentage"];
    NSString *percent = [percentFormat stringFromNumber:percentValue];

    [self updateSectorPopupItems:[result sectorPopUpButton]];
    result.sectorPopUpButton.title = [[self.dataSource objectAtIndex:row] valueForKey:@"sectorName"];
    result.sectorPercentage.stringValue = percent;
    result.percentStepper.objectValue = percentValue;
    result.mySlider.objectValue = percentValue;
    return result;
}

任何可能导致此行为的想法?

修改

确认只有当我创建了足够需要滚动视图的项目时所有tableviewcells都可见时才会出现此行为,然后按预期工作。

1 个答案:

答案 0 :(得分:1)

  1. 首先尝试将NSStepper / Slider的标记设置为" viewForTableColumn"中的行值。方法。与[mySlider setTag: row];

  2. 类似的内容
  3. 然后在您的方法-(IBAction)percentDidChange:(id)sender上适当地投射发件人。 NSSlider *tempSlider = (NSSlider*)sender;

  4. 然后您可以通过

    检索正确的行

    NSInteger row = [tempSlider tag];

  5. 打印值并参阅

相关问题