"Some summary of iOS7" 9. Display content with List (top): List view UITableView

Source: Internet
Author: User

A list view, as the name implies, displays the contents of the data as a list of views on the screen. In iOS, the list view is implemented in UITableView, which is very frequent in practical applications, but not very easy for beginners to understand. Here is a summary of UITableView's main usage for future reference.

UITableView defined in the header file UITableView.h, the specific definition can view the official documents; As can be seen from the definition, UITableView inherits from the Uiscrollview class, so it is also natural to support vertical scrolling operations while supporting easy display of list data. Each element that makes up a list is called a UITableViewCell instance. A UITableViewCell is also a widely used class that defines the official documents that are visible. During specific use, you can create a standalone uitableview, or you can create a uitableviewcontroller directly. The main record here is the method for creating UITableView, and the next record uses UITableView through the list view controller.

The Style property is defined in the UITableView class:

@property (nonatomic, readonly) Uitableviewstyle style
Each oneUITableView can choose one of two types, the grouping mode and the planar mode, which are defined in the enumeration variable Uitableviewstyle:

typedef enum {   uitableviewstyleplain,   uitableviewstylegrouped} uitableviewstyle;

The composition of each list view is similar, consisting of a header view + a body + footer view. Where the table header and footer Two Toomer think nil, you can create a custom view to add to the header and footer when needed. Defined as follows:

@property (nonatomic, retain) UIView *tableheaderview; @property (nonatomic, retain) UIView *tablefooterview;
In addition to the table header and footer, the table body consists of a string of UITableViewCell (hereinafter referred to as cell). If you are grouping table views, multipleUITableViewCell form a section, and each section also has head and tail views.

Here's a simple new demo to show you how to create a uitableview. This assumes that everyone understands the basic operation of Xcode, so it's no longer a step-by-step, simple narrative. Do not know can go to Baidu "Xcode new project."

Create a new single view application, override the Loadview method in the newly generated VIEWCONTROLLER.M file, and create a new UITableView view. (Don't forget to release the Alloc view in the Dealloc function.) )

-(void) loadview{    cgfloat width = [UIScreen mainscreen].bounds.size.width;    CGFloat height = [UIScreen mainscreen].bounds.size.height;        UIView *backgroundview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, width,height)];    Self.view = Backgroundview;        _tableview = [[UITableView alloc] Initwithframe:cgrectmake (0, 0, width, height) style:uitableviewstyleplain];    [Self.view Addsubview:_tableview];    [_tableview release];}

Compile run, display as:


The Protocol method of the table view-This is a very important part, because we create a table view that allows the view to display data, otherwise an empty table view is no more than a waste. The protocol method defined by the table view is composed of the Proxy method delegate and the data source method of the DataSource method. Delegate methods are typically used to personalize the basic style of a table view, such as the height of a cell, and to capture the response of a selected cell; The data source method is used to complete the data in the table, such as specifying the number of cells, and creating each cell.

To implement the proxy and data source methods, you first need to have the current View controller support the Uitableviewdelegate and Uitableviewdatasource protocols. Make the following changes:

@interface viewcontroller:uiviewcontroller<uitableviewdelegate,uitableviewdatasource>
And after the TableView is created, theTableView's delegate and DataSource are set to self, which is delegated to the current view controller to control the data display and response of the table view.

_tableview.delegate = Self;_tableview.datasource = self;

The delegate and data source protocols have two methods that must be implemented:

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath;-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section;
These two methods are used to generate each cell, and specify how many rows are in the current section. Implementing both methods is the minimum requirement that you want the data to be implemented in a table view.

We declare a nsarray *model (retain property) in the View Controller header file and assign [Uifont Familynames] to this property in Viewdidload.

Implement these two proxy methods in the View controller:

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{    return [_model Count] ;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    static NSString *identify = @ "Tableviewcell";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:identify];        if (nil = = cell)    {        cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Identify];        Cell.textLabel.text = Self.model[indexpath.row];    }        return cell;}
In the Cellforrowatindexpath method, the first check is whether there are idle cells, and if there are no unused cells, a new cell is created and returned. The parameter indexpath indicates that the cell currently being created is in the first row of the entire table view.
Compile, run, show results:


If you want to implement a response to a selected cell, you only need to implement the Proxy method below. In the proxy method, you can implement the creation of a new view controller and control its loading onto the screen.

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.