O'Reilly logo

Programming iOS 4 by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required