2014-02-05 22 views
0

我正在使用搜索欄和顯示控制器與我的視圖控制器。當我使用默認的UITableViewCell時,一切正常。但是當我使用控制器的自定義單元格。它不會在拖動的標籤上顯示文字。如何在搜索欄中使用自定義單元格視圖類?

此代碼工作正確─

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

{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"userName"]; 
    } 

    return cell; 
} 

現在我有分配有一個名爲SearchFriendCell和標識SearchFriends類的原型細胞。 SearchFriendCell類包含帶插座的標籤。 現在,如果我用以下代碼替換上面的代碼,它不顯示任何結果。

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

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

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     cell.friendName.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"userName"];// this line doesn't work 
    } 

    return cell; 
} 

在,如果我替換下面一行註釋行修改後的代碼,它顯示result-

cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"userName"]; 

我不知道這裏有什麼問題。搜索欄顯示控制器無法識別我的原型單元。在此之前,我多次使用表視圖控制器,但是我沒有遇到這種類型的問題。 幫助我一些很好的答案。

+0

沒有你連''中的friendName''IBOutlet' SearchFriendCell' – Akhilrajtr

+0

什麼是FRIENDNAME是它是一個UILabel還是隻是NSString?它被添加到單元格內容視圖中嗎? – ahmedalkaff

+0

它是一個真正的原型單元嗎? '[SearchFriendCell alloc] initWithStyle:...'是否被調用過? – Wain

回答

2

您的手機未正確註冊。你說你有一個正確的標識符的原型單元格,但在代碼中你看不到正在創建的單元格的一個實例。這意味着您的代碼將運行:

cell = [[SearchFriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

它不會創建所有子視圖或連接任何插座。

你需要找出爲什麼你的單元格沒有正確註冊。如果你在故事板中創建它,那麼它應該包含在它將被使用的表格視圖中(它應該自動註冊單元格)。如果您在獨立的XIB文件中創建了它,那麼您需要加載NIB並在加載表視圖時顯式註冊它。


你有2個表視圖,該小區被註冊一個而不是其他的 - 這是方法原型細胞起作用。

你最好的選擇:

  1. 取出原型細胞,創建一個XIB並與每個表視圖註冊NIB明確
  2. 不使用搜索控制器(所以你只有1個表視圖,管理搜索欄自己)
+0

我已經在表格視圖上方的故事板中創建了它。是的,我知道它應該自動註冊。不知道爲什麼會發生。在桌子視圖上方' –

+0

'?你能否顯示我檢查過的屏幕截圖 – Wain

+0

。如果我使用沒有搜索欄的表格視圖控制器,它工作正確。但是當我用搜索欄使用它時,它不能識別。 –

0

您需要創建插座,並與你的表視圖加入它

@property (weak, nonatomic) IBOutlet UITableView *myTableView; 
和使用

SearchFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

改用

SearchFriendCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
4

原型細胞是註冊在你的tableview,但在搜索欄中結果的tableview,當你調用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"SearchFriends"; 
    SearchFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
您使用

參數tableView可以是搜索欄結果tableview,它沒有寄存器供你定製原型。因此,只要改變

SearchFriendCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

你可能不再需要

if (cell == nil) { ... } 

最好的問候, JL

相關問題