2017-07-25 53 views
0

下面的代碼應該從數組中調用用戶名,並且用戶可以點擊所需的用戶名。打電話給該陣列是成功的,我可以看到用戶名,但無法使用它。甚至不打印「didSelectRowAt」。感謝你的幫助。謝謝。didSelectRowAt from custom cell do nothing

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! MentionUserCell 

    cell.username!.text = autoComplete[indexPath.row] 

    return cell 
} 

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



func numberOfSections(in tableView: UITableView) -> Int 
{ 
    return 1 
} 


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 

    let selectedCell: MentionUserCell = tableView.cellForRow(at: indexPath)! as! MentionUserCell 
    let selectedWord: String = selectedCell.username!.text! + " " 
    print("didSelectRowAt") 
    var string: NSString = NSString(string: self.commentTextView.text!) 
    string = string.replacingCharacters(in: otoCadang.sharedInstance.foundRange, with: selectedWord) as NSString 

    // change text field value 
    commentTextView.text = string as? String 

    // change cursor position 
    let positionOriginal = commentTextView.beginningOfDocument 
    if let cursorLocation: UITextPosition = self.commentTextView.position(from: positionOriginal, offset: otoCadang.sharedInstance.foundRange.location + selectedWord.characters.count) 
    { 
     self.commentTextView.selectedTextRange = self.commentTextView.textRange(from: cursorLocation, to: cursorLocation) 
    } 



    // remove suggestion 
    self.autoComplete.removeAll() 
    self.tableView.reloadData() 
    tableView.isHidden = true 
} 
+0

你試過調試你的代碼嗎?只需在'didSelectRowAt'中添加一個斷點並檢查發生了什麼...... –

+0

確保表的選擇模式未設置爲'No Selection' –

+0

這是帶有添加的表的「UITableViewController」或「UIViewController」嗎?如果後者,你是否設置了表視圖的「委託」? – rmaddy

回答

0

檢查表

  1. 的tableview委託設置正確
  2. 的tableview選擇模式==不選擇,然後換乘單選
  3. 函數名是正確的func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  4. 你在這個視圖上使用任何點擊手勢?去掉它。
+0

是做到了這一點,成功。似乎是因爲輕拍手勢。謝謝! – mclovin

0

如果您正在編輯UITextView將無法​​調用`didselectRowAt。嘗試在UITextView的方法中獲取Your MentionUserCell,IndexPath並在那裏處理。例如:

func textViewDidChange(_ textView: UITextView) { 
    var parentView = textView as UIView 
    while !(parentView is UITableViewCell) { 
     parentView = parentView.superview! 
     } 
     if let cell: MentionUserCell = parentView as? MentionUserCell { 
      if let indexPath = self.tableView.indexPath(for: cell) { 
       let yourIndexPath = indexPath 
      } 
     } 
     //your code 
} 

希望它能幫助你。

0

當使用兩個或更多個單元時,didSelectRow方法無法正常工作。您可以使用單元格中的按鈕。

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

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

    cell.username!.text = autoComplete[indexPath.row] 


    let button = cell.viewWithTag(101) as! UIButton 
    button.cellclick.addTarget(self, action: #selector(functionName(sender:)), for: .touchDown) 
    return cell 

} 

現在您可以indexPath: -

func functionName(sender:UIButton) { 
    let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tableview) 
    let indexPath = self.tableview.indexPathForRow(at: buttonPosition) 
    print(indexPath.row) 
}