1

我正在尝试将此示例的 ImageAndTextCell.h 类添加到我的项目中。我已经添加成功了。它看起来像下面

这个

在行上单击我得到选定的索引,在 IBAction 上我需要将其更改为绿色。这也很好。但是当我点击另一行时,前面的行绿色变成红色。我确定问题出在检查行的逻辑上。我已经实现了以下失败的代码

 - (NSCell *)tableView:(NSTableView *)tv dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {



NSTextFieldCell  *cell = [tableColumn dataCell];
if(tv== [self tableView]){
    if([[tableColumn identifier] isEqualToString:COLUMN_TITLE]){
        if(![tickTracker count]){
            ImageAndTextCell *imageAndTextCell = (ImageAndTextCell *)cell;
            // Set the image here since the value returned from outlineView:objectValueForTableColumn:... didn't specify the image part...
            NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Cross" ofType:@"png"];
            NSImage *markerImage = [[NSImage alloc] initWithContentsOfFile:fileName];
            [imageAndTextCell setImage:markerImage];

        }else{
             for (int i= 0;i < [tickTracker count];i++){ 
              NSLog(@" 123 number %@",[tickTracker objectAtIndex:i]);
              NSInteger myInteger = [[tickTracker objectAtIndex:i] integerValue];
              [changedRows addObject:[tickTracker objectAtIndex:i]];


            if(row == myInteger){
                NSLog(@" %li value ",row);
                ImageAndTextCell *imageAndTextCell = (ImageAndTextCell *)cell;
                // Set the image here since the value returned from outlineView:objectValueForTableColumn:... didn't specify the image part...
                NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Tick" ofType:@"png"];
                NSImage *markerImage = [[NSImage alloc] initWithContentsOfFile:fileName];
                [imageAndTextCell setImage:markerImage];

            }else{
                 ImageAndTextCell *imageAndTextCell = (ImageAndTextCell *)cell;
                 // Set the image here since the value returned from outlineView:objectValueForTableColumn:... didn't specify the image part...
                NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Cross" ofType:@"png"];
                NSImage *markerImage = [[NSImage alloc] initWithContentsOfFile:fileName];
                [imageAndTextCell setImage:markerImage];
                }
            }
         }
   return cell;
  }

有人可以帮我吗?当它检查索引 1 处的对象时,tickTracker 中索引 0 处的对象未设置为绿色。

编辑 1。在ticktracker内部:

-(IBAction)方法:(id)发件人{

 NSUInteger idx = [[tableView selectedRowIndexes] firstIndex];

 while (idx != NSNotFound) {
    int rowKey = (int)idx;
    NSNumber* rowNumber = [NSNumber numberWithInt:rowKey];
    [tickTracker addObject:rowNumber];

    idx =[[tableView selectedRowIndexes] indexGreaterThanIndex:idx];



}

}

输出:

  NSLog(@" 123 number %@",[tickTracker objectAtIndex:i]);
  Output: 123 number 2
  NSLog(@" %li value ",row);
  2 value 
4

2 回答 2

0

1) 声明一个selectedCarsNSMutableDictionary 类型的属性。初始化它。这将替换您的“tickTracker”。

2)您不需要IBAction。只需覆盖tableViewSelectionDidChange

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
    [selectedCars addObject:@"yes" forKey:[[self tableView] selectedRow];
}

3) 在dataCellForTableColumn替换

}else{
             for (int i= 0;i < [tickTracker count];i++){ 
              NSLog(@" 123 number %@",[tickTracker objectAtIndex:i]);
              NSInteger myInteger = [[tickTracker objectAtIndex:i] integerValue];
              [changedRows addObject:[tickTracker objectAtIndex:i]];


            if(row == myInteger){

}else{
   if ([self.selectedCars objectForKey:row]) {
      ... set the tick image ...
   } else {
      ... set the cross image ...
}

4) 如果要取消选择行,则检查 ...DidChange 委托方法,如果selectedCars已设置并删除该条目,如果为真。

于 2013-01-31T23:54:56.187 回答
0
   if([tickTracker containsObject:row]) { 


    }

这是已解决的问题。只要对象行值在 tickTracker 内,就可以更改图像。

于 2013-02-08T02:38:52.523 回答