2014-03-29 1 views
0

안녕하세요. Json 데이터 테이블에서 UISearchBar 옵션을 구현했습니다. 그러나 검색 할 때마다 프로그램이 중단됩니다. 고맙습니다.xocde의 UISearchBar 5

내 테이블보기 컨트롤러의 헤더 및 구현 파일은 다음과 같습니다.

헤더 파일.

#import <UIKit/UIKit.h> 

@interface byTitleViewController : UITableViewController 

@property (nonatomic, strong) NSMutableArray * jsonArray; 
@property (nonatomic, strong) NSMutableArray * songsArray; 
@property (strong, nonatomic) NSMutableArray* filteredTableData; 

- (void) retriveData; 

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; 
@property (nonatomic, assign) bool isFiltered; 


@end 

구현

#import "byTitleViewController.h" 
#import "songs.h" 

#define getDataURL @"http://fetchpin.com/byTitle.php" 


@interface byTitleViewController() 

@end 

@implementation byTitleViewController 

@synthesize jsonArray, songsArray, filteredTableData, searchBar, isFiltered; 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.title = @"By Title"; 

    searchBar.delegate = (id)self; 

    [self retriveData]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    NSUInteger rowCount; 

    if(self.isFiltered) 
     rowCount = filteredTableData.count; 
    else 
     rowCount = songsArray.count; 

    return rowCount; 
} 

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

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    songs* songlist; 

    if (isFiltered) { 
     songlist = [filteredTableData objectAtIndex:indexPath.row]; 

    }else{ 
     songlist = [songsArray objectAtIndex:indexPath.row]; 
    } 

    cell.textLabel.text = songlist.songTitle; 

    return cell; 

    // Configure the cell... 

    /*songs * songObject; 

    songObject = [songsArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text = songObject.songTitle; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
     */ 




} 


// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 


- (void) retriveData{ 

    NSURL * url = [NSURL URLWithString:@"http://fetchpin.com/byTitle.php"]; 
    NSData * data = [NSData dataWithContentsOfURL:url]; 

    jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

    songsArray = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < jsonArray.count; i++) { 
     NSString * sId = [[jsonArray objectAtIndex:i] objectForKey:@"id"]; 
     NSString * sTitle = [[jsonArray objectAtIndex:i] objectForKey:@"title"]; 

     [songsArray addObject:[[songs alloc]initWithSongTitle: sTitle andSongID: sId]]; 

    } 

    //[self.tableView reloadData]; 

} 

//search bar ... 

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text 
{ 
    if(text.length == 0) 
    { 
     isFiltered = FALSE; 


    } 
    else 
    { 
     isFiltered = true; 
     filteredTableData = [[NSMutableArray alloc] init]; 

     for (songs * sTitle in songsArray) 
     { 
      NSRange titleRange = [sTitle.songTitle rangeOfString:text options:NSCaseInsensitiveSearch]; 

      if(titleRange.location != NSNotFound) 
      { 
       [filteredTableData addObject:sTitle]; 
      } 
     } 
    } 


    [self.tableView reloadData]; 
} 

@end 

답변

0

문제가 당신이 JSON에서로드하는 일부 개체가있는 NSString없는 제목을 가지고있다처럼 보이는 파일하지만 NSNull. textDidChange에 NSNull에 대한 검사를 추가 : 방법으로 문제를 해결해야합니다

if(![sTitle.name isKindOfClass:[NSNull class]]) 
{ 
    NSRange titleRange = [sTitle.name rangeOfString:text options:NSCaseInsensitiveSearch]; 

    if(titleRange.location != NSNotFound) 
    { 
     [filteredTableData addObject:sTitle]; 
    } 
} 

또는, 당신은 당신의 retrieveData 방법에 NSNull 제목과 노래를 필터링 할 수 있습니다.

+0

의견을 보내 주셔서 감사합니다. –