2014-03-29 50 views
0

嗨我在我的Json數據表中實現了UISearchBar選項。但是,無論何時我試圖搜索它,程序都會崩潰。高度讚賞這方面的幫助。UISearchBar在xocde 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。增加對NSNull檢查在textDidChange:方法應該解決的問題:

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

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

另外,還可以過濾掉的歌曲在你retrieveData方法的NSNull稱號。

+0

感謝您的反饋.. –