2011-03-31 83 views
4

我有一个tableView作为我的第一个视图的程序。我也实现了(或至少试图)在视图顶部的搜索栏。用了几个小时来搜索解决方案,但没有获得积极的结果。无效的参数类型为一元表达式

#import "FirstViewController.h" 
#import "NSDictionary-MutableDeepCopy.h" 


@implementation FirstViewController 

@synthesize listData, table, search, allNames, names, keys; 

#pragma mark - 
#pragma mark Custom Methods 

- (void)resetSearch { 
    NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy]; 
    self.names = allNamesCopy; 
    [allNamesCopy release]; 
    NSMutableArray *keyArray = [[NSMutableArray alloc] init]; 
    [keyArray addObjectsFromArray:[[self.allNames allKeys] 
            sortedArrayUsingSelector:@selector(compare:)]]; 
    self.keys = keyArray; 
    [keyArray release]; 
} 

-(void)handleSearchForTerm:(NSString *)searchTerm { 
    NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init]; 
    [self resetSearch]; 

    for (NSString *key in self.keys) { 
     NSMutableArray *array = [names valueForKey:key]; 
     NSMutableArray *toRemove = [[NSMutableArray alloc] init]; 
     for (NSString *name in listData) { 
      if ([name rangeOfString:searchTerm 
       options:NSCaseInsensitiveSearch].location == NSNotFound) 
       [toRemove addObject:name]; 
     } 

     if ([array count] == [toRemove count]) 
      [sectionsToRemove addObject:key]; 

     [array removeObjectsInArray:toRemove]; 

     [toRemove release]; 

    } 
    [self.keys removeObjectsInArray:sectionsToRemove]; 
    [sectionsToRemove release]; 
    [table reloadData]; 

} 


- (void)viewDidLoad {  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; 
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 

    self.names = dict; 
    self.allNames = dict; 

    [dict release]; 

    [self resetSearch]; 
    [table reloadData]; 
    [table setContentOffset:CGPointMake(0.0, 44.0)animated:NO]; 

    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; 

    NSArray *array = [[NSArray alloc] initWithObjects: 

// A larger amount of objects here. 

self.listData = array; 

    [array release]; 
    [super viewDidLoad]; 

} 

/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
// Custom initialization 
} 
return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
[super viewDidLoad]; 
} 
*/ 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 

    self.listData = nil; 
    self.table = nil; 
    self.search = nil; 
    self.allNames = nil; 
    self.names = nil; 
    self.keys = nil; 

} 


- (void)dealloc { 
    [listData release]; 
    [search release]; 
    [table release]; 
    [allNames release]; 
    [keys release]; 
    [names release]; 
    [super dealloc]; 
} 

#pragma mark - 
#pragma mark Table View Data Source Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return ([keys count] > 0) ? [keys count] : 1; 

} 

- (NSInteger)tableView:(UITableView *)aTableView 
numberOfRowsInSection: (NSInteger)section { 
    return [self.listData count]; 
    if ([keys count] == 0) 
     return 0; 
    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 
    return [nameSection count]; 
} 

- (UITableViewCell *) extracted_method: (UITableViewCell *) cell { 
    return cell; 

} 
- (UITableViewCell *)tableView:(UITableView *)aTableView 

cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 

    static NSString *sectionsTableIdentifier = @"sectionsTableIdentifier"; 

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier: 
          sectionsTableIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: sectionsTableIdentifier] autorelease]; 

} 

    cell.backgroundColor = [UIColor clearColor]; 
    cell.textColor = [UIColor whiteColor]; 
    cell.detailTextLabel.textColor = [UIColor whiteColor]; 
    cell.text = [nameSection objectAtIndex:row]; 

    [self extracted_method: cell].text = [listData objectAtIndex:row]; 

    return cell; 

} 

    - (NSString *)tableView:(UITableView *)tableView 
    titleForHeaderInSection:(NSInteger)section { 
    if ([keys count] == 0) 
     return nil; 


    NSString *key = [keys objectAtIndex:section]; 
    return key;  

    } 

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
     return keys; 

    } 

#pragma mark - 
#pragma mark Table View Delegate Methods 
     - (NSIndexPath *)tableView:(UITableView *)tableView 
    willSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
      [search resignFirstResponder]; 
      return indexPath; 
    } 


#pragma mark - 
#pragma mark Search Bar Delegate Methods 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
     NSString *searchTerm = [searchBar text]; 
     [self handleSearchForTerm:searchTerm]; 
} 

- (void)searchBar:(UISearchBar *)searchBar 
textDidChange:(NSString *)searchTerm { 
    if ([searchTerm length] == 0) { 
    [self resetSearch]; 
    [table reloadData]; 
    return; 
} 

    [self handleSearchForTerm:searchTerm]; 
} 


    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
     search.text = @""; 
     [self resetSearch]; 
     [table reloadData]; 
     [searchBar resignFirstResponder]; 

} 
@end 

好吧伙计们。我的问题是,这没有得到搜索功能的工作。此外,我收到交叉口信号SIGABRT在这一行:

NSString *key = [keys objectAtIndex:section]; 

所以我需要有两件事情帮助:

1:我需要获得SIGABRT了。

错误日志消息:*终止应用程序由于未捕获的异常 'NSRangeException',原因: '* - [NSMutableArray的objectAtIndex:]: 索引0超出界限为空数组'

那就是我没有在键中存储任何数据。我会怎么做?我会存储什么?

2:要搜索功能在我的listData数组中搜索!

在此先感谢 - 希望你能帮助!

+1

错误消息表明编译器认为它在看到“ - ”引入您的方法时正在解析表达式。检查你的括号上面这点匹配是否正确。如果他们发布更多的上下文代码。 – CRD 2011-03-31 20:57:40

+0

增加了更多代码。没有完全得到你的意思:) – Mathias 2011-03-31 21:04:13

回答

3

你还没有完成你的sectionIndexTitlesForTableView:方法。现在,它是:

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
     return keys; 

没有结束},所以编译器认为一切后,仍然是方法的一部分。当您尝试定义下一个方法时,请使用使用- (NSIndexPath *)来指示它是返回NSIndexPath *的实例方法,但编译器认为您正在尝试减去某些内容。

解决方法很简单:将}添加到sectionIndexTitlesForTableView:的末尾。

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
     return keys; 
} 
+0

是的,我也想到了我自己。但是,当我在该方法的末尾放置该函数时,它会将程序中的其余行变为错误......请注意您的帮助:) – Mathias 2011-04-01 16:47:23

+0

我真的不知道我的代码 - 应该存储在密钥中??? – Mathias 2011-04-03 23:04:28

+0

仍然需要帮助.. – Mathias 2011-04-04 08:10:46

相关问题