我正在尝试将此示例的 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