下面的代码有效,但可能有更好的方法.我的目标是在从编辑模式中止时从UITableCell调用UIViewController函数.
我这样做是通过为每个UITableViewCell设置一个实例化的UIViewController引用,然后在UITableViewCell状态更改时调用一个函数CancelDelete().
代码似乎效率低下,因为对于每个MyCell,我首先将占位符MyViewContoller实例化为公共变量,然后在UITableView初始化时将其替换为引用UIViewController.
class MyCell : UITableViewCell { var prevIoUsstate : UITableViewCellStateMask = UITableViewCellStateMask.allZeros // This holds a reference to the parent view controller // Seems wasteful to instantiate since it gets replaced var controller:MyViewController = MyViewController() // This is called when the user aborts edit mode override func willTransitionToState(state: UITableViewCellStateMask) { if state & UITableViewCellStateMask.ShowingEditControlMask != nil { if prevIoUsstate & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil { // send notification to controller controller.CancelDelete(self) } } prevIoUsstate = state } } class MyViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // Give cell access to this controller var cell:MyCell = tableView.dequeueReusableCellWithIdentifier("cell") as MyCell cell.controller = self cell.tag = indexPath.row } // This is called from the table cell func CancelDelete(cell:MyCell) { editButtons[cell.tag].hidden = false } }
解决方法
将控制器的类型更改为MyViewController!而不是MyViewController.另外,将其设置为默认值nil.
控制器的声明应如下所示:
var controller: MyViewController! = nil
如果您对以感叹号(!)结尾的类型有任何疑问,请查看:
https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html(在名为Optionals的部分中).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。