2015-06-20 56 views
0

我已經使用下面列出的代碼在uitableview中實現了一個搜索欄(和顯示控制器)。當我輸入搜索文本框時,出現以下sigabrt錯誤:UitableView搜索文本Sigbart錯誤 - ios

由於未捕獲的異常'NSUnknownKeyException',原因:'[valueForUndefinedKey:]:該類不是符合密鑰名稱的密鑰值編碼「。

我已經重新審查了我一直在關注的教程和四重檢查代碼,但是我無法找到錯誤的原因,任何人都可以幫忙嗎?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [self.searchResults count]; 
    } else { 
     return _restaurantsInCity.count; 
    } 
} 

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

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

    RestarauntEntity *currentRestaurant = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     currentRestaurant = [[self.searchResults objectAtIndex:indexPath.row]]; 
    } else { 
     currentRestaurant = [self.restaurantsInCity objectAtIndex:indexPath.row]; 
    } 

    NSString *decodedText1 = [currentRestaurant.title stringByReplacingOccurrencesOfString:@"’" withString:@"'"]; 
    NSString *decodedText = [decodedText1 stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; 
    cell.textLabel.text = decodedText; 
    cell.textLabel.font = [UIFont fontWithName:@"avenir" size:16.0f]; 
    cell.textLabel.textColor=[UIColor blackColor]; 
    cell.detailTextLabel.text = currentRestaurant.city; 
    cell.detailTextLabel.font = [UIFont fontWithName:@"avenir" size:12.0f]; 
    cell.detailTextLabel.textColor=[UIColor darkGrayColor]; 

    return cell; 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText]; 
    searchResults = [_restaurantsInCity filteredArrayUsingPredicate:resultPredicate]; 
} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 
+0

嘗試創建一個異常斷點,並啓用objective-c殭屍對象來更好地瞭解崩潰。 –

回答

0

試試這個

,如果你已經創建了按鈕簡化版,存在更多的營業網點或行動它發生。 檢查你所有的行爲和網點。 轉到故事板並右鍵單擊您的視圖控制器的黃色標誌,並刪除所有帶有黃色標誌的操作和插座。

enter image description here

希望它能幫助。

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

試試這個答案希望它幫助。

+0

謝謝,但我已經嘗試過了,在我的任何一個視圖控制器上都沒有任何動作或插口,這些控制器對他們有一個黃旗。任何其他建議,我花了幾個小時尋找一個解決方案,據我所知,我的代碼是好的,還有什麼可以嗎? – user1419810

+0

謝謝,我已經看了一下,但不知道如何檢查「懸掛連接」,答案中的屏幕截圖沒有顯示與「!」的任何關聯。感嘆號,我在哪裏找到這些? – user1419810

0

如果您無法調試您當前的代碼,那麼您也可以在UITextField幫助下實現搜索欄功能。只需將UITextField而不是搜索欄放在UITableView上並設置委託。確保你正在使用你的數據對象類的變量的正確名稱。我在這裏寫我的代碼,讓我知道如果這有助於

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

if (isFromSearchResult) // BOOL to determine is from search result or not 
{ 
    return [searchResults count]; 
} 
else 
{ 
    return myAppDelegate.offersArr.count; 
} 

}

- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {

customCell = (offersCustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

if(isFromSearchResult) 
{ 
    dataObj = [searchResults objectAtIndex:indexPath.row]; //dataObj is object of NSObject data class 
} else 
{ 
    dataObj = [myAppDelegate.offersArr objectAtIndex:indexPath.row]; 
} 

customCell.LblOffer.text = [dataObj.OfferTitle uppercaseString]; 

NSString *validity = @"VALID TILL: "; // dataObj.ValidTime; 
customCell.LblValid.text = [validity stringByAppendingString:dataObj.ValidTime]; 

return customCell; 

}

- (空)filterContentForSearchText:(的NSString *)SEARCHTEXT範圍:(* NSString的)範圍{// 這裏搜索三個fields-標題,內容和廣告着裝

NSPredicate *predicateOfferTitle = [NSPredicate predicateWithFormat:@"OfferTitle contains[cd] %@", searchText]; 
NSPredicate *predicateOfferContent = [NSPredicate predicateWithFormat:@"OfferContent contains[cd] %@", searchText]; 
NSPredicate *predicateAddress = [NSPredicate predicateWithFormat:@"Address contains[cd] %@", searchText]; 

NSArray *subPredicates = [NSArray arrayWithObjects:predicateOfferTitle, predicateOfferContent,predicateAddress, nil]; 
NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates]; 
searchResults = [myAppDelegate.offersArr filteredArrayUsingPredicate:orPredicate]; 
//NSLog(@"%@", searchResults); 

}

- (BOOL)的TextField:(的UITextField *)的TextField shouldChangeCharactersInRange:(NSRange)範圍replacementString:(的NSString *)串{

NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string]; 
//NSLog(@"value: %@", value); 
if (![value isEqualToString:@""]) { 
    [self filterContentForSearchText:value scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 

    isFromSearchResult=YES; 
} 
else 
{ 
    isFromSearchResult=NO; 
} 

[offersTable reloadData]; 
return true; 

}

+0

謝謝,我剛試過這個,但沒有喜悅,崩潰仍然發生。這太令人沮喪了,令人難以置信! – user1419810