"IOS7 Some summary" 9, with list display (in): List display UITableView

Source: Internet
Author: User

The list is displayed as the name implies, and it is the content of the data displayed on the screen in a list view. To iOS in the list view UITableView reached. This class is frequently used in practical applications and is easy to understand. Here is a summary of UITableView's main use methods for future reference.

UITableView defined in the header file UITableView.h, the detailed definition is able to view the official document; as can be seen from the definition, UITableView inherits from the Uiscrollview class, so it is also natural to support vertical scrolling operations at the same time that support easy display of list data.

Each element that makes up the list is called a UITableViewCell instance.

A UITableViewCell is also a widely used class that defines the official documents that are visible.

During detailed use, it is possible to create a standalone uitableview and to 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
EachUITableView can choose one of two style types. That is, grouping mode and flat mode. Both of these patterns are defined in the enumeration variable Uitableviewstyle:

typedef enum {   uitableviewstyleplain,   uitableviewstylegrouped} uitableviewstyle;

Each list view consists of a similar composition, consisting of a header view + a body + footer view. The table head and the end of the two view Toomer feel nil. When needed, you can create your own definition view to add to the header and footer. Definitions such as the following:

@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). The assumption is grouped table views. The multipleThe UITableViewCell form a section, and each of the sections also has a head and tail view.

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 is no longer a step-by-step, simple narrative can be. Do not know how to go to Baidu "Xcode new project."

Create a new single view application. In the newly generated viewcontroller.m file, rewrite the Loadview method 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 execution. For example, to show a sample of:


Table View protocol Method-This is a very important part, because we create a table view, the purpose is to let the view can display data, otherwise an empty table view and waste no two. The protocol method defined by the table view is composed of the Proxy method delegate and the data source method of the DataSource method.

The delegate method is typically used to personalize the basic style of the table view, such as the height of a cell, and to capture the response selected by the cell. The data source method is used to complete the data in the table. such as the number of cells specified. and create 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, for example:

@interface viewcontroller:uiviewcontroller<uitableviewdelegate,uitableviewdatasource>
And after the TableView is created, theTableView's delegate and DataSource are set to self, which is entrusted 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 separately. and specify how many rows the current section collectively owns.

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 attribute) 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, you first check for unused cells, assuming there are no unused cells. A new cell is created and returned. The indexpath indicates that the cell currently being created is in the first row of the entire table view.
Compile. Execute, Show results:


Assuming that you want to implement a response to a selected cell, you only need to implement the following proxy methods. In the proxy method, it is possible to create a new view controller and control its loading onto the screen.

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

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

"IOS7 Some summary" 9, with list display (in): List display UITableView

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.