参加NSTableview的每一行以更改Click上的图像 -

时间:2013-01-31 19:11:42

标签: macos nsmutablearray nstableview nstableviewcell

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

this

在行上单击我获取所选索引,在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;
  }

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

编辑1 。    在滴答记录中:

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

2 个答案:

答案 0 :(得分:0)

1)声明NSMutableDictionary类型的属性selectedCars。初始吧。这取代了你的“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,则删除该条目为真。

答案 1 :(得分:0)

   if([tickTracker containsObject:row]) { 


    }

这个已解决的问题。只要对象行值在tickTracker中,就会更改图像。