I'm trying to add this example's ImageAndTextCell.h class to my project. I have added it successfully. It looks something like below
On row click i get the selected index and on IBAction I need to change it to green color. Which works fine too. But when I click on another row, the previous rows green turns to red. Im sure the problem is with the logic of checking for the row. I have implemented the below code which failed
- (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;
}
Can someone help me on this? The object at index 0 in tickTracker is not set to green when it checks for object at index 1.
EDIT 1 . Inside the ticktracker:
-(IBAction)method:(id)sender{
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];
}
}
Ouput:
NSLog(@" 123 number %@",[tickTracker objectAtIndex:i]);
Output: 123 number 2
NSLog(@" %li value ",row);
2 value
1) Declare a property selectedCars
of type NSMutableDictionary. Init it. This replaces your "tickTracker".
2) You don't need an IBAction. Just override tableViewSelectionDidChange
:
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
[selectedCars addObject:@"yes" forKey:[[self tableView] selectedRow];
}
3) In dataCellForTableColumn
replace
}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){
with
}else{
if ([self.selectedCars objectForKey:row]) {
... set the tick image ...
} else {
... set the cross image ...
}
4) If you want to deselect rows, then check in the ...DidChange delegate method, if selectedCars
is already set and delete the entry if true.
if([tickTracker containsObject:row]) {
}
This the settled issue. Aslong as the object row value is inside the tickTracker change the image.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.