ios - 如何创建两个自定义表格单元格按钮?
<p><p>我正在准备一张表格,当我在其中滑动单元格时,我需要获得两个圆形按钮。每个按钮应该有一个图像和一个标签。</p>
<pre><code>override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> ? {
var hello = UITableViewRowAction(style: .Default, title: "Image") { (action, indexPath) in
// do some action
if let buttonImage = UIImage(named: "Image") {
//self.bgColor = UIColor.imageWithBackgroundColor(image: buttonImage, bgColor: UIColor.blueColor())
}
return editButtonItem()
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,你的代码有一些问题:</p>
<ol>
<li>您返回 <code>editButtonItem()</code> 方法的结果,该方法基本上丢弃了您的 <code>hello</code> 操作。我将根据它的名称假设此方法返回一个操作,而不是您想要的两个。</li>
<li>在您的操作处理程序中,您尝试在 <code>self</code> 上设置背景。 block 从其父范围捕获变量,因此该 block 中的 <code>self</code> 与 <code>hello</code> 操作无关,而是与您的 <code>editActionsForRowAtIndexPath</code> 所在的类有关> 方法已实现。</li>
</ol>
<p>如何实现你所需要的(带标题和图片的两个按钮):</p>
<pre><code>override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> ? {
var firstAction = UITableViewRowAction(style: .Default, title: "First") { (action, indexPath) in
// action handler code here
// this code will be run only and if the user presses the button
// The input parameters to this are the action itself, and indexPath so that you know in which row the action was clicked
}
var secondAction = UITableViewRowAction(style: .Default, title: "Second") { (action, indexPath) in
// action handler code here
}
firstAction.backgroundColor = UIColor(patternImage: UIImage(named: "firstImageName")!)
secondAction.backgroundColor = UIColor(patternImage: UIImage(named:"secondImageName")!)
return
}
</code></pre>
<p>我们创建了两个单独的 Action ,分配它们的背景颜色以使用图案图像,并返回一个包含我们两个 Action 的数组。这是改变 <code>UITableViewRowAction</code> 外观所能做的最多的事情 - 我们可以看到 <a href="https://developer.apple.com/reference/uikit/uitableviewrowaction" rel="noreferrer noopener nofollow">from the docs</a> ,这个类不继承自 <code>UIView</code>。</p>
<p>如果您想更多地自定义外观,您应该寻找外部库或从头开始实现您自己的解决方案。</p></p>
<p style="font-size: 20px;">关于ios - 如何创建两个自定义表格单元格按钮?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39504430/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39504430/
</a>
</p>
页:
[1]