ios - Notify UITableViewController when cell is programmatically selected -
my table view allows multiple cell selection, each cell sets selected when button inside cell has been clicked (similar gmail app does, see picture below). looking way let uitableviewcontroller
know cells have been selected or deselected, in order manually change uinavigationitem
. hoping there way using delegate methods, cannot seem find one. didselectrowatindexpath
handling clicks on cell itself, , should not affect cell's selected state.
the straight forward way create our own delegate protocol
cell, uitableviewcontroller
adopt. when dequeue cell, set delegate
property on cell uitableviewcontroller
instance. cell can invoke methods in protocol
inform uitableviewcontroller
of actions occurring , can update other state necessary. here's example code give idea (note did not run compiler, there may typos):
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 appropriate } func articlecelldidbecomeunselected(articlecell: articlecell) { // update state 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 } }
Comments
Post a Comment