Table View Selection
A table view cell has a normal (deselected) state and a selected state, according to its selected
property. It is possible to change a cell’s selected
property directly (possibly with animation, using setSelected:animated:
), but you are more likely to manage selection through the table view. Indeed, one of the chief purposes of your table view is likely to be to let the user select a cell. This will be possible, provided you have not set the value of the table view’s allowsSelection
property to NO. The user taps a normal cell, and the cell switches to its selected state. As we’ve already seen, this will usually mean that the cell is redrawn with a blue (or gray) background.
Your code can also learn and manage the selection through these UITableView instance methods:
-
indexPathForSelectedRow
- Reports the currently selected row, or nil if there is no selection.
-
selectRowAtIndexPath:animated:scrollPosition:
The animation involves fading in the selection, but the user may not see this unless the selected row is already visible. The last parameter dictates whether and how the table view should scroll to reveal the newly selected row:
-
UITableViewScrollPositionTop
-
UITableViewScrollPositionMiddle
-
UITableViewScrollPositionBottom
-
UITableViewScrollPositionNone
For the first three, the table view scrolls (with animation, if the second parameter is YES) so that the selected row is at the specified position among the visible cells. For
UITableViewScrollPositionNone
, the ...-
Get Programming iOS 4 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.