0

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

我的 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

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

4

2 回答 2

0

You made a mistake

you don't initialize UISearchBar in your -(void)viewDidLoad method

just put this line into -(void)viewDidLoad

  searchBar = [[UISearchBar alloc] init];

EDIT :

For more information about UISearchBar Refer this Link

& for Documentation about UISearchBar Refer this http://developer.apple.com/library/ios/#documentation/uikit/reference/UISearchBar_Class/Reference/Reference.html

于 2013-01-19T03:09:57.920 回答
0

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

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

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

于 2013-01-19T08:21:35.107 回答