是否可以调用类似于选择行的内容 Button1(messageContact)或button2(sendFriendRequest)
然后让它执行两个单独的功能,具体取决于您在行的同一单元格内单击的按钮?
本
答案 0 :(得分:1)
您可以为您的单元格操作制作代理:
protocol UserCellDelegate: class {
func didTapSendMessage(_ sender: UserCell)
func didTapAddSendFriendRequest(_ sender: UserCell)
}
并在UserCell上添加此行:
var delegate: UserCellDelegate?
因此,当用户点击sendFriendRequest或messageContact时,您可以执行以下操作:
@IBAction func sendMessageTapped(_ sender: UIButton) {
delegate?.didTapSendMessage(self)
}
@IBAction func sendMessageTapped(_ sender: UIButton) {
delegate?.didTapSendRequest(self)
}
然后从您的视图控制器确认该代表:
class UserViewController: UserCellDelegate {
func didTapSendMessage(_ sender: UserCell) {
guard let tappedIndex = tableView.indexPath(for: sender),
let user = users[tappedIndex.section].content[tappedIndex.row] as? User else {
return
}
//Do what you need in order to send message to user
}
func didTapAddSendFriendRequest(_ sender: UserCell) {
guard let tappedIndex = tableView.indexPath(for: sender),
let user = users[tappedIndex.section].content[tappedIndex.row] as? User else {
return
}
//Do what you need in order to send friend request
}
}
不要忘记将自己设置为单元格的委托:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ...
cell.delegate = self
}