UITabelView 与 UISearchDisplayDelegate

发布于 2024-12-03 06:15:59 字数 4054 浏览 5 评论 0原文

我正在尝试使用 UISearchDisplayDelegate 设置一个简单的 UITableView,但我的应用程序不断崩溃并出现 SIGABRT 错误: [self.filteredListContent removeAllObjects];

关于为什么会发生这种情况的任何想法

#import "SearchViewController.h"

@implementation SearchViewController

@synthesize listContent, filteredListContent;




- (void)viewDidLoad
{
    [super viewDidLoad];


    listContent = [[NSArray alloc]initWithObjects:@"hello", @"bye", nil];
    filteredListContent = [NSMutableArray arrayWithCapacity:listContent.count];

    [self.tableView reloadData];
    self.tableView.scrollEnabled = YES;


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.listContent =nil;
    self.filteredListContent=nil;

}

-(void) dealloc {

        [super dealloc];
        [self.listContent release];
        [self.filteredListContent release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.filteredListContent count];
    }
    else
    {
        return [self.listContent count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    NSString *label;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        label = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        label = [self.listContent objectAtIndex:indexPath.row];
    }    


    cell.textLabel.text = label;    
    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}



- (void)filterContentForSearchText:(NSString*)searchText 
{

    [self.filteredListContent removeAllObjects]; 
    for (NSString *string in listContent)
    {
            NSComparisonResult result = [string compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [self.filteredListContent addObject:string];
            }

    }
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];
    return YES;

}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text]];
    return YES;

}

头文件

#import <UIKit/UIKit.h>

@interface SearchViewController : UITableViewController<UISearchDisplayDelegate, UISearchBarDelegate>  {
    NSArray                       *listContent;
    NSMutableArray        *filteredListContent;

}

@property (nonatomic, retain) NSArray *listContent;
@property (nonatomic, retain) NSMutableArray *filteredListContent;



@end

I'm trying to set up a simple UITableView with a UISearchDisplayDelegate but my app keeps crashing with a SIGABRT error for : [self.filteredListContent removeAllObjects];

Any ideas on why this is happening

#import "SearchViewController.h"

@implementation SearchViewController

@synthesize listContent, filteredListContent;




- (void)viewDidLoad
{
    [super viewDidLoad];


    listContent = [[NSArray alloc]initWithObjects:@"hello", @"bye", nil];
    filteredListContent = [NSMutableArray arrayWithCapacity:listContent.count];

    [self.tableView reloadData];
    self.tableView.scrollEnabled = YES;


}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.listContent =nil;
    self.filteredListContent=nil;

}

-(void) dealloc {

        [super dealloc];
        [self.listContent release];
        [self.filteredListContent release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.filteredListContent count];
    }
    else
    {
        return [self.listContent count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    NSString *label;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        label = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        label = [self.listContent objectAtIndex:indexPath.row];
    }    


    cell.textLabel.text = label;    
    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}



- (void)filterContentForSearchText:(NSString*)searchText 
{

    [self.filteredListContent removeAllObjects]; 
    for (NSString *string in listContent)
    {
            NSComparisonResult result = [string compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
            {
                [self.filteredListContent addObject:string];
            }

    }
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];
    return YES;

}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text]];
    return YES;

}

header file

#import <UIKit/UIKit.h>

@interface SearchViewController : UITableViewController<UISearchDisplayDelegate, UISearchBarDelegate>  {
    NSArray                       *listContent;
    NSMutableArray        *filteredListContent;

}

@property (nonatomic, retain) NSArray *listContent;
@property (nonatomic, retain) NSMutableArray *filteredListContent;



@end

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

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

发布评论

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

评论(1

剑心龙吟 2024-12-10 06:15:59

替换

filteredListContent = [NSMutableArray arrayWithCapacity:listContent.count];

self.filteredListContent = [NSMutableArray arrayWithCapacity:listContent.count];

or

filteredListContent = [[NSMutableArray alloc] initWithCapacity:listContent.count];

这是因为当您直接访问变量时,不会调用 retain 。始终使用 self. 表示法。

Replace

filteredListContent = [NSMutableArray arrayWithCapacity:listContent.count];

with

self.filteredListContent = [NSMutableArray arrayWithCapacity:listContent.count];

or

filteredListContent = [[NSMutableArray alloc] initWithCapacity:listContent.count];

This is because when you access your variable directly retain is not called. Always use self. notation.

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