Frage

I have an action for searching in NSMutableArray with name searcharray, which is equal to array with which NSTableView are connected.And I wrote a method for adding items to TableView just by sending NSMutableArray to my method. The problem is that after searching if I delete what I have typed in SearchField and SearchField is empty, the compiler doesn't feel that it's empty and my TableView getting empty too, but due my code it's have to be with data from searcharray. Here is my code:

#import "myTableViewAppDelegate.h"

@implementation myTableViewAppDelegate

@synthesize window;
@synthesize searcharray;


-(void)addItems:(NSMutableArray *)ar{
    array = ar;
    [array retain];
    [tableView reloadData];
}
- (int)numberOfRowsInTableView:(NSTableView *)tableView{
    return (int)[array count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
            row:(int)row
{
    return [array objectAtIndex:row];
}



- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSMutableArray *animals = [[NSMutableArray arrayWithObjects:@"Cat", @"Dog", @"Fish", @"Squirrel", @"Bear", @"Turtle", nil] retain];
    NSLog(@"%@",animals);
    searcharray = animals;
    [self addItems:animals];

}

- (IBAction)search:(id)sender {
    //NSLog(@"%@",searcharray);
    NSString *filter = [[NSString alloc] initWithString:[search stringValue]];
    NSMutableArray *result = [[NSMutableArray alloc] init];
    if (filter != nil) {
      for (NSString *item in searcharray) {
        if ([item rangeOfString:[search stringValue]].location !=NSNotFound ) {
            NSLog(@"Item %@ contains %@",item,[search stringValue]);
            [result addObject:item];
        }
    }  
    }
    else{
    result = searcharray;
    }
    NSLog(@"%@",result);
    [self addItems:result];
    [result release];
    [filter release];
}
@end
War es hilfreich?

Lösung

Fixed it.

if (filter != nil || filter.length != 0)

I don't know why, but just checking variable equal to NULL wasn't enough....

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top