我試圖按下tableview上的按鈕時執行某些操作。我添加的按鈕以在故事板的單元以及連接它這樣:按鈕目標不會執行TableViewCell中的函數
@IBOutlet weak var likeMessage: UIButton!
然後,我添加了一個目標和在所述cellForRowAt的按鈕標籤。
cell.likeMessage.tag = indexPath.row
cell.likeMessage.addTarget(self, action: #selector(SignalRViewController.handleLikes(sender:)), for:.touchUpInside)
而且我在同一個類(SignalRViewController)中有handleLikes函數。
func handleLikes(sender: UIButton){
print("was clicked")
}
我發現其他職位這一方式,但 我無法得到它的工作。我錯過了什麼?
更新
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.tableViewMessage.backgroundView = nil
//My Messages
guard let cell = tableView.dequeueReusableCell(withIdentifier: "myMessageCell", for: indexPath) as? MyMessageTableViewCell else {
fatalError("The dequeued cell is not an instance of Community")
}
cell.imgUser.layer.cornerRadius = cell.imgUser.frame.height/2
cell.imgUser.clipsToBounds = true
cell.imgUser.layer.borderWidth = 3.0
cell.lblMessage.lineBreakMode = .byWordWrapping // or NSLineBreakMode.ByWordWrapping
cell.lblMessage.numberOfLines = 0
cell.background.layer.cornerRadius = 7
cell.lblMessage.text = "\(arrayMessage[indexPath.row][0])"
cell.lblMessage.textAlignment = .left
cell.imgUser.layer.borderColor = UIColor(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).cgColor
cell.background.backgroundColor = constants.duckEggBlue
cell.imgUser.downloadedFrom(link: userImageServer)
cell.lblTimestamp.text = getFormattedTime(index: indexPath.row, cell: "myCell")
cell.likeMessage.tag = indexPath.row
cell.likeMessage.addTarget(self, action: #selector(handleLikes(sender:)), for:.touchUpInside)
return cell}
請寫上UITableViewCell類,方法handleLikes。 – shahnilay86
嘗試使用它,因爲它是在同一個類中: 'cell.likeMessage.addTarget(self,action:#selector(handleLikes(sender :)),for:.touchUpInside) –
@saroshmirza沒有工作以及 –