【发布时间】:2019-02-11 10:03:47
【问题描述】:
我使用UITableView 创建了简单的聊天。我想添加长按后突出显示消息的功能。事实上,我想创建和 iMessage 一样的功能:
长按后,我们取消突出显示背景(更暗),突出显示消息,滚动到此消息并显示actionSheet
目前我只添加了longPress 和actionSheet
viewDidLoad 上的长按识别器:
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(onCellLongPressed(gesture:)))
messagesTableView.addGestureRecognizer(longPressRecognizer)
onCellLongPressed函数:
@objc func onCellLongPressed(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began {
let touchPoint = gesture.location(in: self.messagesTableView)
if let indexPath = messagesTableView.indexPathForRow(at: touchPoint) {
self.messagesTableView.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)
shareWithFriend()
}
}
}
@objc func shareWithFriend() {
alert(style: .actionSheet, actions: [
UIAlertAction(title: "Share with friend", style: .default, handler: { [weak self] (_) in
print("SHARE HERE")
}),
UIAlertAction(title: "Cancel", style: .destructive),
])
}
func alert(_ title: String? = nil, message: String? = nil, style: UIAlertController.Style, actions: [UIAlertAction]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: style)
actions.forEach(alertController.addAction)
present(alertController, animated: true)
}
【问题讨论】:
-
关于滚动,我建议使用以下代码: tableView?.scrollToRow(at: [0,0], at: UITableViewScrollPosition.top, animated: true) 只需将 [0,0] 替换为索引路径
-
@DJ-Glock 谢谢!突出显示选定的单元格呢?
-
让我检查一下我的代码。我已经实现了类似的东西。
-
我已经发布了我的答案。请注意,我没有对其进行测试,但希望对您有所帮助。
-
我做了一些更新。感谢您的反馈。
标签: ios swift uitableview