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:

Reports the currently selected row, or nil if there is no selection.

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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.