ios - 如何以编程方式触发 UITableViewCell editActions?
<p><div>
<aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
<div class="d-flex fd-column fw-nowrap">
<div class="d-flex fw-nowrap">
<div class="flex--item wmn0 fl1 lh-lg">
<div class="flex--item fl1 lh-lg">
<b>这个问题在这里已经有了答案</b>:
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>简短的回答是 - 没有这样的方法。</p>
<p>但是,如果您真的需要类似的东西,您可以模仿这种行为,尽管它需要您自己进行更多的实现和状态处理。</p>
<p>这是一个快速且非常肮脏的解决方案,它覆盖了自定义单元格的 <code>touchesEnded</code> 方法。请记住在相关 Storyboard 的表格 View 中将 Cell 设置为单元格的动态原型(prototype),并将其重用标识符设置为 <code>identitifer</code>。</p>
<pre><code>import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CellDelegate {
@IBOutlet weak var tableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "identifier") as? Cell else {
return UITableViewCell()
}
cell.textLabel?.text = "\(indexPath.row)"
cell.indexPath = indexPath
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> ? {
return nil
}
func doAction(for cell: Cell) {
let indexPath = cell.indexPath
print("doing action for cell at: \(indexPath!.row)")
// your implementation for action
// maybe delete a cell or whatever?
cell.hideFakeActions()
}
}
protocol CellDelegate: class {
func doAction(for cell: Cell)
}
class Cell: UITableViewCell {
weak var delegate: CellDelegate?
var indexPath: IndexPath!
@IBOutlet weak var buttonAction1: UIButton?
@IBOutlet weak var constraintButtonFakeActionWidth: NSLayoutConstraint?
override func awakeFromNib() {
self.constraintButtonFakeActionWidth?.constant = 0
}
override func touchesEnded(_ touches: Set<UITouch>,
with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else {
return
}
if self.point(inside: point, with: event) {
print("event is in cell number \(indexPath.row)")
self.showFakeActions()
}
}
func showFakeActions() {
self.constraintButtonFakeActionWidth?.constant = 80
UIView.animate(withDuration: 0.3) {
self.layoutIfNeeded()
}
}
func hideFakeActions() {
self.constraintButtonFakeActionWidth?.constant = 0
UIView.animate(withDuration: 0.3) {
self.layoutIfNeeded()
}
}
@IBAction func fakeAction() {
delegate?.doAction(for: self)
}
}
</code></pre>
<p>那么它是如何工作的呢?每个单元格都是一个继承自抽象 <code>UIResponder</code> 接口(interface)的 <code>UIView</code>。您可以覆盖其方法以代表分派(dispatch)给它们的事件自行执行操作。这是覆盖 <code>touchesEnded</code> 的第一步。</p>
<p>看一下界面生成器的屏幕截图 - 你必须连接约束。
<a href="/image/gLwEB.png" rel="noreferrer noopener nofollow"><img src="/image/gLwEB.png" alt="enter image description here"/></a> </p>
<p>我还实现了对单元格的所有编辑操作返回 nil 的委托(delegate),因此它们不会干扰您的解决方法。</p>
<p>接下来,您设置一个自定义委托(delegate)以从单元格中获取回调。为了方便管理 dataSource 中的数据,我还将 IndexPath 附加到单元格,您必须实现。</p>
<p>请记住,这段代码缺少很多,比如 <code>prepareForReuse</code> 方法的实现。此外,您可能希望在 <code>touchesEnded</code> 覆盖中进行额外检查,以保证每次触摸不会多次触发此委托(delegate)回调并防止多次触摸。此处也未实现在单元格上禁用用户交互的逻辑。并且界面需要微调(比如在动画过程中文本似乎被压扁了)。</p></p>
<p style="font-size: 20px;">关于ios - 如何以编程方式触发 UITableViewCell editActions?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47223092/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47223092/
</a>
</p>
页:
[1]