2017-01-06 37 views
2

我無法讓didSelectRowAt方法在常規ViewController內工作TableView。我已經確定該表的delegatedata source設置在ViewController代碼中。這ViewController填充tableview細胞與從搜索查詢結果的API,和小區數據的繪製工作正常。無法使didSelectRowAt爲TableView工作

這只是didSelectRowAt方法未註冊。我也嘗試手動添加在Main.storyboard相同的委託信息,但小+標誌將不會觸發任何彈出窗口。我想知道是否有需要修復的Main.storyboard中的東西。我還附上了ViewControllerTableView連接檢查員的圖像。我對iOS開發很陌生,對移動設計的圖形界面沒有太多經驗,所以我認爲它是那裏的東西,但也許我錯了。

這裏是我的代碼的基礎版本:

class SearchViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource { 


@IBOutlet weak var tableView: UITableView! 
@IBOutlet var searchBar: UISearchBar! 

    ...variable declarations .... 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.hideKeyboardWhenTappedAround() 
    searchResults = [] 
    searchBar.delegate = self 
    tableView.dataSource = self 
    tableView.delegate = self 

} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1; 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return searchResults!.count; 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "searchTableViewCell", for: indexPath) as! SearchTableViewCell 

    if(searchActive && !(self.searchResults?.isEmpty)!) { 
     (doing some stuff with search results here...works fine) 
    } 
    return cell 
} 

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

func searchBar(_ searchBar: UISearchBar, 
       textDidChange searchText: String) { 
    print("search text \(searchText)") 
    getSearchResultJSON(term: searchText) { json in 
     let data = json as! [Dictionary<String, String>] 
     self.searchResults = data 
    } 
    self.tableView.reloadData() 
    } 
... 
} 

[Viewcontroller connections inspector]

[TableView Connections Inspector]

編輯:的健全性檢查,如果搜索異步函數改變什麼,我只是嘗試刪除所有與搜索相關的代碼,並從硬編碼的虛擬變量數組中填充tableview。它的工作是顯示虛擬變量,但仍然沒有能力選擇一個單元格並得到任何反應。我還看到一對夫婦提到,我以前有一個與didDeSelectRowAt而不是didSelectRow在一個錯字,已修復,但行爲是相同的。

這最終被涉及到發生在我寫

+0

嘗試刪除委託聲明在viewDidLoad方法和你接管表格單元格的任何tapgesture或按鈕來設置的tableview委託在故事板 –

+0

? –

+2

您正在使用didDeselectRowAt方法。 – Niharika

回答

2

找到了!罪魁禍首是self.hideKeyboardWhenTappedAround(),這是我寫的隱藏鍵盤的擴展。這干擾了一個單元的水龍頭,因爲它確實利用了UITapGestureRecognizer。感謝大家的提示。

2

的hideKeyboardWhenTappedAround()擴展您使用didDeselectRowAt代替didSelectRowAt

編輯

好了,用這個點擊手勢以下代表然後

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 

a第二讓如果你正在使用的主視圖,然後表視圖單元格並選擇方法工作不正常雙擊手勢控制器符合UIGestureRecognizerDelegate

+0

我希望我能說這是答案,因爲它非常有意義!但是我只是做了編輯,單元格仍然沒有響應選擇 – jeanmw

+0

單擊單元格時看不到「hello」? – Matt

+0

不,沒有打印的消息@Matt – jeanmw

0

0

在圖片您上傳,委託和數據源沒有連接到ViewController。 刪除viewdidload(tableview.delegate = self)中的代碼並將它們連接到故事板。

相關問題