如何将委托函数转换为 IBAction 函数
How to Translate a Delegated Function to a IBAction Function
我有一个委托函数,可以在点击时更改 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 函数工作吗?
好的,这是我以前用过的解决方法。
看起来不漂亮,但很管用:
UIButton *likeButton = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)likeButton.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
只需为 UICollectionView Class 类型的按钮找到正确的超级视图。
P.S。 - 我的 Swift 不是最好的。希望你能转换这个。
好的,所以您的操作来自按钮,该按钮位于 table 视图单元格内,问题是:该 table 视图单元格的 indexPath 是什么。
最简单的方法是:
- 获取按钮在 table 视图中的位置;
- 请求那个位置的索引路径。
例如
@IBAction func checkOffTask(sender: UIButton!) {
let originInTableView = self.tableView.convertPoint(CGPointZero, fromView: sender)
let indexPath = self.tableView.indexPathForRowAtPoint(originInTableView)
}
我有一个委托函数,可以在点击时更改 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 函数工作吗?
好的,这是我以前用过的解决方法。 看起来不漂亮,但很管用:
UIButton *likeButton = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)likeButton.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
只需为 UICollectionView Class 类型的按钮找到正确的超级视图。
P.S。 - 我的 Swift 不是最好的。希望你能转换这个。
好的,所以您的操作来自按钮,该按钮位于 table 视图单元格内,问题是:该 table 视图单元格的 indexPath 是什么。
最简单的方法是:
- 获取按钮在 table 视图中的位置;
- 请求那个位置的索引路径。
例如
@IBAction func checkOffTask(sender: UIButton!) {
let originInTableView = self.tableView.convertPoint(CGPointZero, fromView: sender)
let indexPath = self.tableView.indexPathForRowAtPoint(originInTableView)
}