如何将委托函数转换为IBAction函数 [英] How to Translate a Delegated Function to a IBAction Function
问题描述
我有一个委托函数,改变UITableViewCell的部分位置点击:
//在单元格上点击,展开
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 b $ b}
}
我希望此操作发生在我触发从按钮点击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函数的任何想法?
好的,所以你的操作来自一个按钮,该按钮是在一个表视图单元格,问题是:
最简单的方法是:
- 在表格视图中获取按钮的位置;
- 请求该位置的索引路径。
Eg
@IBAction func checkOffTask(sender:UIButton!){
let originInTableView = self.tableView .convertPoint(CGPointZero,fromView:sender)
let indexPath = self.tableView.indexPathForRowAtPoint(originInTableView)
}
I have a delegate function that changes a UITableViewCell's section location on tap:
//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()
}
}
I would like for this action to occur when I fire an IBAction from a button tap, but I'm not sure how I can access the indexPath argument as is given in the delegate function. Here is my current code for the IBAction button:
@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()
}
Any idea on how to get the delegate function to work for the IBAction function?
Okay, so your action is coming from a button, that button is within a table view cell, the question is: what's the indexPath of that table view cell.
The easiest way is:
- get the location of the button within the table view;
- ask for the index path at that position.
E.g.
@IBAction func checkOffTask(sender: UIButton!) {
let originInTableView = self.tableView.convertPoint(CGPointZero, fromView: sender)
let indexPath = self.tableView.indexPathForRowAtPoint(originInTableView)
}
这篇关于如何将委托函数转换为IBAction函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!