NSTableview 和 SearchField
我有一个名为 searcharray 的 NSMutableArray 搜索操作,它等于与 NSTableView 连接的数组。我编写了一个方法,只需将 NSMutableArray 发送到我的方法即可将项目添加到 TableView 中。 问题是,在搜索后,如果我删除了在 SearchField 中输入的内容并且 SearchField 为空,编译器不会觉得它是空的,我的 TableView 也变空了,但由于我的代码,它必须包含来自 searcharray 的数据。 这是我的代码:
#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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修好了。
我不知道为什么,但仅仅检查变量等于 NULL 是不够的......
Fixed it.
I don't know why, but just checking variable equal to NULL wasn't enough....