【发布时间】:2017-08-08 13:23:32
【问题描述】:
当按下 tableview 上的按钮时,我正在尝试执行某些操作。我将按钮添加到故事板中的单元格并像这样连接它:
@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。
-
尝试使用它,因为它属于同一类:
cell.likeMessage.addTarget(self, action: #selector(handleLikes(sender:)), for:.touchUpInside) -
@saroshmirza 效果不佳
-
@Fabioha 你能把整个代码sn-p分享到
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -
@saroshmirza 我用整个代码 sn-p 更新了帖子
标签: ios swift uitableview uibutton