一元表达式的参数类型无效

发布于 2024-10-28 23:59:58 字数 6965 浏览 2 评论 0原文

我有这个程序,其中一个 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数组中搜索!

预先感谢 - 希望你能提供帮助!

I have this program with a tableView as my first view. I have also implemented (or at least tried to) a search bar on top of the view. Have used several hours to search for a solution, but without positive results.

#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

Ok guys. My problem is that this doesnt get the search function to work. In addition I receive siginal SIGABRT at this line:

    NSString *key = [keys objectAtIndex:section];

So I need help with two things:

1: I need to get that SIGABRT away.

Error log message: * Terminating app due to uncaught exception
'NSRangeException', reason: '*
-[NSMutableArray objectAtIndex:]:
index 0 beyond bounds for empty array'

That is I don't store any data in keys. how would I do that? and what would I store?

2: Want the search function to search in my listData array!

Thanks in advance - hope u can help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-11-04 23:59:58

您尚未完成 sectionIndexTitlesForTableView: 方法。现在是:

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

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

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

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

You have not finished your sectionIndexTitlesForTableView: method. Right now it is:

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

There is no closing }, so the compiler thinks everything after that is still part of that method. When you try to define the next method, use use - (NSIndexPath *) to indicate that it is an instance method which returns NSIndexPath*, but the compiler thinks you are trying to subtract something.

The solution is simple: Add the } to the end of sectionIndexTitlesForTableView:.

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return keys;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文