我目前正試圖按照本教程進行搜索。 http://www.appcoda.com/how-to-add-search-bar-uitableview/ 但是,我在我的應用程序崩潰,但我不知道,如何解決它的點。在TableView上執行搜索後,應用程序崩潰
JSON來到我的應用程序,目前只有一行數據(我無法更改它,因爲我沒有數據庫訪問)。
我的初始視圖看起來不錯。 TableView被填充(1行)。當我點擊搜索欄並放入一些不屬於該項目名稱的字符時,我會得到正常的「未找到結果」屏幕。 當我輸入任何字符,這是在項目的名稱中存在,它顯示我按預期過濾項目。但是,當我輸入第二個字母,即項目名稱時,我的應用程序崩潰,在控制檯中輸出以下輸出。
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
現在我的代碼。 numberOfRowsInSection方法根據搜索進行修改。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[resultSearch valueForKeyPath:@"data.auctions"]count];
}
}
numberOfSectionsInTableView只有一個部分硬編碼(返回1,因爲我目前沒有更多的需要)
現在我的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; //custom cell
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *aucdict = [jsonAukResultsSearch objectAtIndex:indexPath.row]; //assigning current row from NSMutableArray(jsonAukResultsSearch) to Dictionary, this is probably crash spot
NSURL *imageURL = [NSURL URLWithString:[aucdict objectForKey:@"img_url"]];
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//sending request to get picture
UIImage *imageLoad = [[UIImage alloc] initWithData:result];
NSString *timeString = [aucdict objectForKey:@"time_left"];
NSString *priceString = [aucdict objectForKey:@"current_value"];
// Configure the cell...
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.nameLabel.text = [aucdict objectForKey:@"name"];
}
//cell.thumbnailImageView.image = imageLoad;
//commented while crashes are happening
return cell;
}
現在,我的應用程序崩潰後,我與
NSDictionary *aucdict = [jsonAukResultsSearch objectAtIndex:indexPath.row];
是熒光筆,所以我猜,這可能是地方,其中indexPath pa rameter超出界限。我真的不明白指數(1)如何超越界限(1)。我知道,我只有零個或一個返回行(取決於搜索短語)。
我剛開始今天的工作搜索,所以我沒有任何線索,什麼那些棘手的斑點。任何建議表示讚賞:)謝謝,鹽池
編輯:好了,所以我發現我的地方問題可能是......我緊跟着這個問題的頂部提到的教程..這種方法應該返回數組與搜索結果。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [poleMien filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%u",[searchResults count]);
}
我加的NSLog,你可以看到...現在,當我加入的第一個字母,匹配,控制檯輸出爲1(因爲它應該是),所以我的篩選結果會顯示沒有問題。然而,當我輸入另一個匹配的字母時,我的searchResults數組將增加到2(就像它會計算「匹配」字母一樣),這可能是原因,爲什麼我會崩潰(嘗試訪問索引1,而我只有一個數據在我的搜索結果數組)
之後,我改變了我的NSLog顯示數組的內容...插入匹配的字母后,相同的項目被添加到我的數組(所以我有另一個項目在我的數組中,有相同的數據作爲我的陣列中的第一項)
編輯2:好吧,所以我的問題是,每當我從字典保存json數據到數組(以便我可以使用數組進行篩選結果)我在cellForRowAtIndexPath中執行它..所以我的數組每次都獲得相同的值,搜索表是「重新填充的」。現在我需要找到一些方法只有在數據不同的情況下才將數據加載到數組中。
好,指標(1)超出範圍(1),因爲一個NSArray在它只有一個元素應該只有在索引的元素(0 )。所以發生的是,當只有一個元素(在索引0處)時,表視圖詢問其數據源爲/ second /元素(索引1)。如果表格視圖不知道只有一行,就不應該這樣做。你可以在你的程序的某個地方設置一個斷點,看看你的tableview返回的是什麼 - [UITableView numberOfSections]和 - [UITableView numberOfRowsInSection:],看看它們是否是你期望的值? – eyebrowsoffire
我的問題是,我不確定在哪裏設置斷點....在我有機會真正將某些內容放入searchBar之前會達到斷點(這是因爲我的tableview與所有項目都會事先加載。 nslog這些計數,但沒有任何顯示在控制檯 – Yanchi
我想我接近解決這個問題...我 - (無效)filterContentForSearchText:(NSString *)searchText作用域:(NSString *)作用域填充我的數組與更多的索引我添加字母到searchBar – Yanchi