当我开始输入时,UISearchBar不起作用

时间:2013-01-19 03:00:21

标签: iphone uitableview uisearchbar

我有一个搜索栏但是当我开始输入不起作用时,应用程序不会崩溃,但搜索栏不会显示任何内容。

我的TableViewController.h文件:

#import <UIKit/UIKit.h>
#import "DetailController.h"

@interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UINavigationItem *barra;
@property (strong,nonatomic) NSArray* jsonObject;
@property (strong,nonatomic) NSMutableArray* jsonObject2;
@property (nonatomic,strong) NSString* conexion;
@property (nonatomic,strong) NSString* titul;

- (IBAction)back:(id)sender;

@end

我的TableViewController.m文件

#import "TableViewController.h"
#import "CostumCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <QuartzCore/QuartzCore.h>

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize myTableView;
@synthesize searchBar;
@synthesize barra;
@synthesize jsonObject;
@synthesize jsonObject2;
@synthesize conexion;
@synthesize titul;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//
//load the JSON object
barra.title=titul;
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:conexion]];
NSInputStream *stream = [[NSInputStream alloc] initWithData:response];
[stream open];
//
if(stream)
{
    NSError *parseError = nil;
    jsonObject = [NSJSONSerialization JSONObjectWithStream:stream options:NSJSONReadingAllowFragments error:&parseError];
    jsonObject2 = [[NSMutableArray alloc]initWithArray:jsonObject];
}
//
self.myTableView.delegate=self;
self.myTableView.dataSource=self;
self.searchBar.delegate=self;

[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [jsonObject count];
}

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

if(!cell){
    cell=[[CostumCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.label.text = [[jsonObject objectAtIndex:indexPath.row]objectForKey:@"nombres"];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[jsonObject objectAtIndex:indexPath.row]objectForKey:@"imagenes"]]placeholderImage:[UIImage imageNamed:@"Untitled-1.png"]];
[cell.imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[cell.imageView.layer setBorderWidth: 2.0];
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marcadeagua.jpg"]];
[tempImageView setFrame:self.myTableView.frame];
self.myTableView.backgroundView = tempImageView;
self.myTableView.separatorColor= [UIColor blackColor];

return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
    DetailController *destViewController = segue.destinationViewController;
    destViewController.nombre=[[jsonObject objectAtIndex:indexPath.row]objectForKey:@"nombres"];
    destViewController.telefonos=[[jsonObject objectAtIndex:indexPath.row]objectForKey:@"telefonos"];
    destViewController.imagen=[[jsonObject objectAtIndex:indexPath.row]objectForKey:@"imagendetalles"];
    destViewController.conexion=[[jsonObject objectAtIndex:indexPath.row]objectForKey:@"conexion"];
    destViewController.value=indexPath.row;
}
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if([searchText length] == 0){
    [jsonObject2 removeAllObjects];
    [jsonObject2 addObjectsFromArray:jsonObject];
}else{
    [jsonObject2 removeAllObjects];
    for (NSDictionary *item in jsonObject) {
            NSString *string = [item objectForKey:@"nombres"];
            NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                [jsonObject2 addObject:item];
        }
    }
}
[myTableView reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)asearchBar{

[searchBar resignFirstResponder];

}

- (void)viewDidUnload
{
[self setMyTableView:nil];
[self setSearchBar:nil];
[self setBarra:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)back:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end

我尝试过不同的代码,但没有任何作用。

2 个答案:

答案 0 :(得分:0)

你犯了一个错误

您未在-(void)viewDidLoad方法中初始化 UISearchBar

将此行放入-(void)viewDidLoad

  searchBar = [[UISearchBar alloc] init];

编辑:

有关 UISearchBar 的详情,请参阅此Link

&安培;有关 UISearchBar 的文档,请参阅此http://developer.apple.com/library/ios/#documentation/uikit/reference/UISearchBar_Class/Reference/Reference.html

答案 1 :(得分:0)

不是你问题的答案,而是这一行:

NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:conexion]];

在主线程上执行,这是一个很大的不。 initWithContentsOfURL阻止线程直到完成下载,并且每次呈现此视图控制器时都会导致应用冻结一秒钟。