2016-06-11 38 views
0

我的表格视图允许多个单元格选择,其中每个单元格将单元格中的按钮单击后将其设置为选中状态(类似于gmail应用程序的功能,请参阅下图)。我正在寻找一种方法让UITableViewController知道已选择或取消选中单元格,以便手动更改UINavigationItem。我希望有办法通过使用委托方法来做到这一点,但我似乎无法找到一个。 didSelectRowAtIndexPath正在处理单元本身的点击,并且不应该影响单元格的选定状态。当以编程方式选择单元格时通知UITableViewController

enter image description here

回答

3

最直接的方式做到这一点是创建我们自己的代表为你的,那你的UITableViewController将采纳。当您将您的单元格出列时,您还将单元格上的delegate属性设置为UITableViewController实例。然后,单元格可以调用中的方法来通知UITableViewController正在发生的操作,并且可以根据需要更新其他状态。下面是一些示例代码给这个想法(请注意,我没有编译器运行它,所以有可能是拼写错误):

protocol ArticleCellDelegate { 
    func articleCellDidBecomeSelected(articleCell: ArticleCell) 
    func articleCellDidBecomeUnselected(articleCell: ArticleCell) 
} 

class ArticleCell: UICollectionViewCell { 
    @IBAction private func select(sender: AnyObject) { 
     articleSelected = !articleSelected 

     // Other work 

     if articleSelected { 
      delegate?.articleCellDidBecomeSelected(self) 
     } 
     else { 
      delegate?.articleCellDidBecomeUnselected(self) 
     } 
    } 

    var articleSelected = false 
    weak var delegate: ArticleCellDelegate? 
} 

class ArticleTableViewController: UITableViewController, ArticleCellDelegate { 
    func articleCellDidBecomeSelected(articleCell: ArticleCell) { 
     // Update state as appropriate 
    } 

    func articleCellDidBecomeUnselected(articleCell: ArticleCell) { 
     // Update state as appropriate 
    } 

    // Other methods ... 

    override tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueCellWithIdentifier("ArticleCell", forIndexPath: indexPath) as! ArticleCell 
     cell.delegate = self 

     // Other configuration 

     return cell 
    } 
} 
1

我会像在视图控制器和'cellButtomDidSelect'功能cellForRowAtIndexPath',将目标动作设置为上述功能

相关问题