About Table Views in iOS-Based Applications

来源:互联网 发布:mysql修改表名 编辑:程序博客网 时间:2024/04/27 19:50

About Table Views in iOS-Based Applications

A table view presents a scrollable list of items that may be divided into sections. Table views are versatile user-interface objects frequently found in iOS-based applications, particularly productivity applications. They have many purposes:

  • To let users navigate through hierarchically structured data

  • To present an indexed list of items

  • To display detail information and controls in visually distinct groupings

  • To present a selectable list of options

Figure I-1  Table views of various kinds

A table view has only one column and allows vertical scrolling only. It consists of rows in sections. Each section can have a header and a footer that displays text or an image. However, many table views have only one section with no visible header or footer. Programmatically, UIKit identifies rows and sections through their index number: Sections are numbered 0 through n-1 from the top of a table view to the bottom; rows are numbered 0 through n-1 within a section. A table view can have its own header and footer, distinct from any section; the table header appears before the first row of the first section, and the table footer appears after the last row of the last section.Programmatically, a table view is an instance of UITableView in one of two basic styles, plain or grouped. A plain-style table view is an unbroken list; a grouped table view has visually distinct sections. A table view has a data source and might have a delegate. These objects provide the data for populating the sections and rows of the table view and customize its appearance and behavior.

Related chapters: “Table View Styles and Accessory Views”

At a Glance

Table Views are Composed From Cells in a Particular Style

A table view draws its visible rows using cells—that is, UITableViewCell objects. Cells are views that can display text, images, or other kinds of content. They can have background views for both normal and selected states. Cells can also have accessory views, which function as controls for selection or setting an option.

The UIKit framework defines four standard cell styles, each with its own layout of the three default content elements: main label, detail label, and image. You may also create your own custom cells to acquire a distinctive style for your application’s table views.

Related Chapters: “Table View Styles and Accessory Views,” “A Closer Look at Table-View Cells”

There Are Several Ways to Create a Table View But the Basic Pattern is the Same

You create an instance of UITableView in one of the two styles and assign it a data source. Immediately after it’s created, the table view asks its data source for the number of sections, the number of rows in each section, and the table-view cell to use to draw each row. The data source manages the application data used for populating the sections and rows of the table view.

The easiest and recommended way to create a table view is to create a custom UITableViewController object. UITableViewControllercreates the table view and assigns itself as delegate and data source. If your application is largely based on table views, use the Navigation-based Application template when you create the application project; this template includes an initial custom UITableViewController class and a separate Interface Builder nib file for the table view.

You can create a table view with the help of Interface Builder or entirely in code. You can use a custom UIViewController object or even a non-controller object to manage a table view.

Related Chapters: “Navigating a Data Hierarchy With Table Views,” “Creating and Configuring a Table View”

Responding to Selections of Rows

When users select a row by tapping it, the delegate of the table view is informed via a message. The delegate is passed the indexes of the row and the section the row is in. It uses this information to locate the corresponding item in the application’s data model. This item might be an intermediate level in the hierarchy of data or a “leaf node" in the hierarchy. If the former, the application displays a new table view; if the later, the application displays detail about the selected item in a grouped-style table view or some other kind of view.

In table views that list a series of options, tapping a row simply selects its associated option. No subsequent view of data is displayed.

Related Chapters: “Navigating a Data Hierarchy With Table Views,” “Managing Selections”

In Editing Mode You Can Add, Delete, and Reorder Rows

Table views can enter an editing mode wherein the user can insert or delete rows, or relocate them within the table. In editing mode, rows that are marked for insertion or deletion display a green plus sign (insertion) or a red minus sign (deletion) near the left edge of the row. If the user touches a deletion control or, in some table views, swipes across a row, a red Delete button appears to prompt the user for deletion of that row. Rows that can be relocated display near their right edge an image consisting of several horizontal lines. When the table view leaves editing mode, the insertion, deletion, and reordering controls disappear.

When users attempt to insert, delete, or reorder rows, the table view sends a sequence of messages to its data source and delegate to allow them to manage these operations.

Related Chapters: “Inserting and Deleting Rows and Sections,” “Managing the Reordering of Rows”

How to Use This Document

To get a concise overview of the programmatic interfaces involved in table-view creation and management, see “Overview of the Table View API.”

See Also

The information presented in this introduction and in “Table View Styles and Accessory Views” summarizes prescriptive information on table views presented in iOS Human Interface Guidelines. It is recommended that you read this for a complete description of the styles and characteristics of table views, as well as their recommended uses.

Before reading this book, you should read iOS App Programming Guide to understand the basic process for developing iOS-based applications. You should also consider reading View Controller Programming Guide for iOS for general information about view controllers and for more detailed information about navigation controllers, which are frequently used in conjunction with table views.

You will find the following sample-code projects to be instructive models for your own table view implementations:

  • TableViewSuite project

  • TheElements project

  • SimpleDrillDown project


原创粉丝点击