IOS development-simple table View

Source: Internet
Author: User

Table view is the most frequently used view in iOS development. We usually choose to display data in the form of tables, such as address book and channel list. The table view's functions such as segmentation, grouping, and indexing make the data we present look more regular and more conditioned. what's even more exciting is that the table view can also display data at multiple levels using detailed display and other functions, A table wins thousands of words. However, compared with other controls, table views are more complex to use. However, compared with the various flexible functions of table views, our effort in use is quite worthwhile. The form of a simple table view is flexible and changeable. Based on the Principle of simplicity, we will first learn from the simple table view. The simple table view described in this section is a dynamic table. (before iOS 5, dynamic tables are all different from static tables ). After creating a simple table view in iOS 5, we can use xib or storyboard technology to create a table view. to display a basic table, we only need to implement the required methods in the UITableViewDataSource Protocol, namely tableView: numberOfRowsInSection: And tableView: cellForRowAtIndexPath.: Constructor: initWithFrame: it is called when the table view is instantiated. If xib or storyboard is used to design the table view, the table view is created when the table View Controller is instantiated. When the table view is displayed, tableView: numberOfRowsInSection: the message is sent to query the number of rows in the current section, when a table view cell is displayed, tableView: cellForRowAtIndexPath is sent to provide display data for the cell. We create a simple table view. The Cell uses the default style and has icons and primary titles, displaying information about the World Cup team. Use the "Single View Application" template to create a project named "SimpleTable". Open the IB design screen and select "View Controller" in "View Controller Scene" to delete the Controller, drag a "Table View Controller" from the control library to the design screen. Modify the parent class of ViewController in the H file from the original UIViewController to UITableViewController. Select "Table View Controller Scene"> "Table View Controller" from the Scene list on the left side of the IB design screen to enable the identity checker of the Table View Controller, select "ViewController" in the Class option, which is our own view controller. Select "Table View Controller Scene"> "Table View Controller"> "Table View Controller"> "Table View" in the Scene list to open the attribute checker of the Table View. Content has two options: "Dynamic Prototypes" and "Static Cells", which are available only in the storyboard. "Dynamic Prototypes" is used to create a "Dynamic table". If code is used to create Cells, set the "Prototype Cells" project to 0. The code implementation mode code is as follows: [cpp] static NSString * CellIdentifier = @ "CellIdentifier"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} static NSString * CellIdentifier = @ "CellIdentif Ier "; UITableViewCell * cell = [tableView failed: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: Invalid reuseIdentifier: CellIdentifier];} identifier is a reusable cell Identifier, which is the same as the reusable cell concept in the Collection view. First, check whether there are reusable cells in the table view. If not, use initWithStyle: reuseIdentifier: constructor to create a cell object. If you want to design Cells Using the storyboard, select "Table View Controller Scene"> "Table View Controller"> "Table View Cell ", open the cell property checker. There are many options in Style. Identifier refers to reusable cell identifiers. After this operation, the cell does not need to be instantiated in the code. We can directly retrieve the cell instance through the set Identifier to reuse the cell. [Cpp] static NSString * CellIdentifier = @ "CellIdentifier"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} We need to set "team. plist and team image are added to the project, ViewController. the hfile code is as follows: # import <UIKit/UIKit. h> @ interface ViewController: UITableViewControll Er @ property (nonatomic, strong) NSArray * listTeams; @ end static NSString * CellIdentifier = @ "CellIdentifier"; UITableViewCell * cell = [tableView worker: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} We need to set "team. plist and team image are added to the project, ViewController. the hfile code is as follows: # import <UIKit/UIKi T. h> @ interface ViewController: UITableViewController @ property (nonatomic, strong) NSArray * listTeams; @ end needs to change the parent class of ViewController to UITableViewController. The listTeams attribute of the NSArray * type is also defined. listTeams is used to load the data read from the file. Read the attribute list file team. the plist operation is the ViewController implemented in the viewDidLoad method. the viewDidLoad method code of the m file is as follows: [cpp]-(void) viewDidLoad {[super viewDidLoad]; NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource: @ "team" ofType: @ "plist"]; // obtain all data in the property list file self. listTeams = [[NSArray alloc] initWithContentsOfFile: plistPath];} Let's look at the UITableViewDataSource protocol method. The Code is as follows:-(NSInteger) tableView :( UITableView *) TableView numberOfRowsInSection :( NSInteger) section {return [self. listTeams count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "CellIdentifier "; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UI TableViewCellStyleDefault reuseIdentifier: CellIdentifier];} NSUInteger row = [indexPath row]; NSDictionary * rowDict = [self. listFilterTeams objectAtIndex: row]; cell. textLabel. text = [rowDict objectForKey: @ "name"]; NSString * imagePath = [rowDict objectForKey: @ "image"]; imagePath = [imagePath stringByAppendingString :@". png "]; cell. imageView. image = [UIImage imageNamed: imagePath]; cell. accessoryType = Response; return cell;}-(void) viewDidLoad {[super viewDidLoad]; www.2cto.com NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource: @ "team" ofType: @ "plist"]; // obtain all data in the property list file self. listTeams = [[NSArray alloc] initWithContentsOfFile: plistPath];} Let's look at the UITableViewDataSource protocol method. The Code is as follows:-(NSInteger) tableView :( UITableView *) tabl EView numberOfRowsInSection :( NSInteger) section {return [self. listTeams count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "CellIdentifier "; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableVi EwCellStyleDefault reuseIdentifier: CellIdentifier];} NSUInteger row = [indexPath row]; NSDictionary * rowDict = [self. listFilterTeams objectAtIndex: row]; cell. textLabel. text = [rowDict objectForKey: @ "name"]; NSString * imagePath = [rowDict objectForKey: @ "image"]; imagePath = [imagePath stringByAppendingString :@". png "]; cell. imageView. image = [UIImage imageNamed: imagePath]; cell. accessoryType = UITa BleViewCellAccessoryDisclosureIndicator; return cell;} because the current table actually has only one section, you do not need to distinguish the Section. In tableView: numberOfRowsInSection: the length of the listTeams attribute is directly returned in the method. TableView: cellForRowAtIndexPath: The row Method of the NSIndexPath parameter in the method to obtain the row index of the current cell. The cell. accessoryType attribute sets the extended view type. We can replace the cell style UITableViewCellStyleDefault with the other three to experience the effects of the other three cell styles. Simple Table case running result

Related Article

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.