2017-02-19 73 views
0

我有一個自定義UITableViewViewController。我的自定義表有自定義UITableViewCells。這是我ViewController裏面的代碼:UITableView didSelectRowAt不註冊

ViewDidLoad

 self.firstListTable.dataSource = self 
     self.firstListTable.delegate = self 
     self.firstListTable.register(UINib(nibName: "firstQueueCell", bundle: Bundle.main), forCellReuseIdentifier: "firstCell") 

     self.searchListTable.dataSource = self 
     self.searchListTable.delegate = self 
     self.searchListTable.allowsSelection = true 
     self.searchListTable.register(UINib(nibName: "SearchCell", bundle: Bundle.main), forCellReuseIdentifier: "SearchCell") 
     self.searchListTable.separatorStyle = .none 

UITableViewFunctions

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { 
    if tableView == self.searchListTable { 
     print("hello") 
    } 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 
    print("hello") 
} 

didHighlightRowAt作品,但didSelectRowAt沒有。

我嘗試這樣做: didSelectRowAtIndexPath: not being called

什麼其他原因可能有多大?

謝謝!

+0

看起來你的代碼沒有錯誤,你可以發佈所有關於它的代碼讓我們看看如果是其他地方出錯了嗎? – aircraft

+0

@ user82395214,檢查你的代碼。它沒有錯。他們都在迴應。你可以把'print('hello)'''''''''''''''print'語句更改爲不同的東西,然後再次檢查它嗎? – KrishnaCA

回答

0

如果你想didHighlightRowAtdidSelectRowAt都響應,您可以覆蓋你的方法didHighlightRowAt這樣的:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { 
if tableView == self.searchListTable { 
    print("hello") 

    // this is my OC code ,change it to your swift code 
    [tableView selectRowAtIndexPath:IndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

    } 
} 

你可以理解爲這didHighlightRowAt方法和didSelectRowAt法有衝突,如果希望兩個方法的反應,你必須做一些特殊的處理。

+0

哦,對不起,我不想說我的'didHighlight'有效,但'didSelect'沒有。我不希望用戶必須持有一個單元格才能與之交互。 – user82395214

+0

我知道你想要什麼,但是如果像你的代碼覆蓋這兩種方法,'didSelectRowAt'很難迴應。用戶必須點擊很長時間來調用'didSelectRowAt'。 –

+0

你能告訴我在'didHighlightRowAt'中你想要的真正努力嗎?我建議你將代碼放在'didSelectRowAt'方法中。 –

-1
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath.row == 2) { 
     //if you want didSelectRowAtIndexPath not call return nil; 
     return nil; 
    } 
    return indexPath; 
}