【发布时间】:2015-09-21 16:30:25
【问题描述】:
我有一个委托函数,可以在点击时更改 UITableViewCell 的部分位置:
//On cell tap, expand
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0
{
let data = firstDataSource[indexPath.row]
tableView.beginUpdates()
secondDataSource.append(data)
firstDataSource.removeAtIndex(indexPath.row)
let newIndexPath = NSIndexPath(forRow: find(secondDataSource, data)!, inSection: 1)
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
tableView.endUpdates()
} else if indexPath.section == 1 {
let data = secondDataSource[indexPath.row]
tableView.beginUpdates()
firstDataSource.append(data)
secondDataSource.removeAtIndex(indexPath.row)
let newIndexPath = NSIndexPath(forRow: find(firstDataSource, data)!, inSection: 0)
tableView.moveRowAtIndexPath(indexPath, toIndexPath: newIndexPath)
tableView.endUpdates()
}
}
我希望通过点击按钮触发 IBAction 时执行此操作,但我不确定如何访问委托函数中给出的 indexPath 参数。这是我当前的 IBAction 按钮代码:
@IBAction func checkOffTask(sender: UIButton) {
var checkedOff = false
let indexOfCell = sender.tag
sender.setImage(UIImage(named: "checkbox-checked"), forState: UIControlState.Normal)
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfCell, inSection: 1)], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()
}
知道如何让委托函数为 IBAction 函数工作吗?
【问题讨论】:
-
行动来自谁?
IBAction只是目标/动作模式的图形连接版本,通常源自 iOS 中的UIControl子类。 -
动作来自 UIButton 视图。
-
那么您希望它与哪一行相关联?当前选择的一个?
-
是的,每一行都有自己的按钮,所以每当点击该按钮时,该特定行应该更改部分位置。
-
你的问题不是已经用
NSIndexPath(forItem: indexOfCell, inSection: 1)解决了吗?
标签: ios swift uitableview cocoa-touch uikit