2016-02-19 55 views
0

我正在研究一個外部動態可可庫(dylib),它在某些時候提供了一個帶有tableview和搜索欄的視圖。 我有這個在上創建搜索欄導致應用崩潰在我的ios庫

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]]; 
    UINib *nib = [UINib nibWithNibName:CONTACT_CELL bundle:frameworkBundle]; 
    [[self tableView] registerNib:nib forCellReuseIdentifier:CONTACT_CELL]; 
    [self readContacts]; 
} 

和我tablerow的委託

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

    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CONTACT_CELL]; 
    if (cell == nil) 
    { 
     NSLog(@"Cell was empty!"); 
    } 

    Contact *c = [contacts objectAtIndex:[indexPath row]]; 

    [[cell labelName] setText: [c getName]]; 
    [[cell labelPhone] setText: [c getPhone]]; 

    return cell; 
} 

問題是,當我點擊搜索欄的應用chrashes因爲:

*斷言失敗 - [UISearchResultsTableView _configureCellForDisplay:forIndexPath:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/ UITableView.m:7962 2016-02-19 17:07:00.404 HostApp [2233:20181] *終止應用程序由於 未捕獲異常'NSInternalInconsistencyException',原因: 'UITableView(; layer =; contentOffset:{0,0}; contentSize:{414,528}>) 未能從其DataSource獲得細胞()」


如果我使用

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath]; 

*斷言故障 - [UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:6564 2016-02-19 17:37:08.254 HostApp [2691: *終止應用程序由於 未捕獲的異常「NSInternalInconsistencyException」,原因:「無法 出列具有標識符ContactCell細胞 - 必須註冊一個筆尖或 使標識符的一類或一 故事板連接原型細胞」

+0

http://stackoverflow.com/questions/34250595/failed-to-obtain-a-cell-from-its-datasource? – Larme

+0

我試着把'[self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:CONTACT_CELL]',但沒有運氣。 – Picci

回答

0

問題是我註冊的筆尖爲「正常」的tableView,但不適合searchdisplaycontroller的的tableView。

使用此viewDidLoad將解決此問題。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSBundle *frameworkBundle = [NSBundle bundleForClass:[self class]]; 
    UINib *nib = [UINib nibWithNibName:CONTACT_CELL bundle:frameworkBundle]; 
    [[self tableView] registerNib:nib forCellReuseIdentifier:CONTACT_CELL]; 
    [self.searchDisplayController.searchResultsTableView registerNib:nib forCellReuseIdentifier:CONTACT_CELL]; 
    [self readContacts]; 
} 
0

您不能在cellForRowAtIndexPath中返回nil

您可以使用:

[tableView dequeueReusableCellWithIdentifier:CONTACT_CELL forIndexPath: indexPath]; 

它總是返回一個細胞,或手動ALLOC它:

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

    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CONTACT_CELL]; 
    if (cell == nil) 
    { 
     NSLog(@"Cell was empty!"); 
     cell = [UITableViewCell alloc] initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(nullable NSString *)#>]; 
    } 

    Contact *c = [contacts objectAtIndex:[indexPath row]]; 

    [[cell labelName] setText: [c getName]]; 
    [[cell labelPhone] setText: [c getPhone]]; 

    return cell; 
} 
+0

看看我的編輯 – Picci

+0

'你的viewDidLoad中'UINib * nib = [UINib nibWithNibName:CONTACT_CELL bundle:frameworkBundle]'的結果是什麼? – tgyhlsb

+0

非空。我不得不這樣實現,因爲自定義單元格xib不會像everiday那樣加載(因爲我正在實現一個外部庫)。 – Picci

0

檢查 1.search條委託你回來什麼 - (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar {} 2.檢查段落tableview刪除中的行數

0

嘗試lik E本

ItemCell是我nibName不是標識符

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil]; 
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"ItemCell"]; 
    } 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
      // Create an instance of ItemCell 
    PointsItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"]; 

     return cell; 
} 
+0

您的viewDidLoad不適用於我,因爲我在框架中編寫代碼而不是我的主應用程序(bundle:nil不工作)。 – Picci