我正在将MCSwipeTableViewCell框架用于可滑动的tableviewcell。cellForRowAtIndexPath
函数内部的完成块之一如下所示:
[cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
// run some function call
}];
我使用Bridging-Header文件将框架导入到我的Swift项目中,并尝试在Swift中使用相同的完成块。这就是我所拥有的:
cell.setSwipeGestureWithView(crossView, color: UIColor.colorFromRGB(RED), mode: MCSwipeTableViewCellMode.Switch, state:MCSwipeTableViewCellState.State1, completionBlock: { (cell: MCSwipeTableViewCell!, state: MCSwipeTableViewCellState!, mode: MCSwipeTableViewCellMode!) -> Void in
self.runSomeFunction();
});
问题是,self.runSomeFunction()
即使函数调用已实现,它也会在每次运行时崩溃。错误是
无法识别的选择器
sent to instance 0x165c7390
2014-07-07 16:23:14.809 pong[3950:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM runSomeFunction]: unrecognized selector sent to instance 0x165c7390'
我知道完成模块是有效的,因为我可以从中获取NSLog并显示某些内容,但是尝试访问self总是会导致崩溃。
有任何想法吗?我不应该尝试进入自我吗?
===更新===
我主要要弄清楚的是如何self
在Swift闭包中进行访问。它总是抛出错误的访问错误。
这是正在运行的代码
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell = tableView.dequeueReusableCellWithIdentifier("userCell") as MCSwipeTableViewCell!
if !cell {
cell = MCSwipeTableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "userCell")
}
cell.setSwipeGestureWithView(crossView, color: UIColor.colorFromRGB(RED), mode: MCSwipeTableViewCellMode.Switch, state:MCSwipeTableViewCellState.State1, completionBlock: { (cell: MCSwipeTableViewCell!, state: MCSwipeTableViewCellState!, mode: MCSwipeTableViewCellMode!) -> Void in
self.runSomething();
});
return cell
}
func runSomething()
{
NSLog("hey there");
}
您可以定义一个捕获列表以self
在闭包内部使用,如下所示:
cell.setSwipeGestureWithView(crossView, color: UIColor.colorFromRGB(RED), mode: MCSwipeTableViewCellMode.Switch, state:MCSwipeTableViewCellState.State1) {
[unowned self]
cell, state, mode in
self.runSomething()
}
目前[unowned self]
有时可能会崩溃,因此暂时[weak self]
无法使用,并且在您的封包内部展开self
如下:self!.doSomething()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句