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

あなたの質問に対する答えではありませんが、この行:

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

メインスレッドで実行され、それは大したことではありません。initWithContentsOfURLダウンロードが完了するまでスレッドをブロックしているため、このビュー コントローラーを提示するたびにアプリが 1 秒間フリーズします。

于 2013-01-19T08:21:35.107 に答える