swift开发笔记22 在表格的单元格中实现条件跳转
如上图所示,在表格的单元格中有个五角星按钮,有选中和未选中两种状态,当选中时要跳转到写评论页面,取消选中则不跳转,那么就需要按条件跳转。 如果跳转的话用拖一个segue就可以了,条件跳转如下文所示:http://blog.csdn.net/baixiaozhe/article/details/50854415 问题是这个按钮的处理事件写在单元格类中:RewardButtonTableViewCell: UITableViewCell 而UITableViewCell 不是一个viewcontroller,没有performSegueWithIdentifier方法,单元格的父容器是UITableViewController,他有performSegueWithIdentifier,那么需要在单元格类中获取父容器,有人用UITableViewCell.superview.superview获取UITableView的方式获取,亲测不行,只好用土办法. 在单元格类中加一个类属性,即父容器的引用:parentView class RewardButtonTableViewCell: UITableViewCell { weakvar parentView:UITableViewController? // 五角星按钮的点击事件处理函数 @IBAction func rewardAction(sender: AnyObject) { parentView?.performSegueWithIdentifier("addRewardComm",sender: self) } } 在父容器UITableViewController的override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath)中,给单元格的类属性赋值:
let cell = tableView.dequeueReusableCellWithIdentifier("RewardCell",forIndexPath: indexPath) as! RewardButtonTableViewCell cell.parentView=self
然后就可以在单元格里控制跳转了。。。。。。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |