Under the Bridge

Code: UITableView multiple selection

My, we do seem to be writing a lot about UITableView recently, don’t we? But this is a particularly good one — how to implement multiple-row selection and actions:

UITableView does not support multiple selection. We will use the method tableView:didSelectRowAtIndexPath: to detect touches in rows but the selected state will need to be stored separately (we cannot rely on the UITableView‘s selection).

We will also need a background view for displaying the selection color and a UIImageView for displaying the not-selected/selected indicator. Since the UIImageView will be hidden while not editing and the label for the row needs to move left or right when it is shown or hidden, we will also need to implement some form of layout for the UITableViewCell.

Other required behaviors include switching the “Edit”/”Cancel” buttons between modes, showing/hiding the toolbar at the bottom and tracking the number of selected rows to display in the button in the toolbar…

Mighty handy stuff, that. Code can be downloaded here. And from quickly flipping around, looks like the entire CocoaWithLove blog this comes from is worth your perusal, there’s quite a number of nifty pieces of esoterica therein.

h/t: iPhoneKicks!

0