Simple use of the table view for iOS learning

Source: Internet
Author: User

Table View Simple Description:

In many iphone and other iOS programs will see the appearance of Table View, in addition to the general table data display, the set of property data is often used in the table view,table View is mainly divided into the following two kinds:

    • Plain: This is an ordinary list style
    • Grouped: This is a chunking style.
For UITableView, we have some special concepts and terminology, such as a cell that we act as a table view, and many cells can form sections, and each section has headers and footer on top and bottom. Many of the sections are composed of the entire table, of course table also has headers and footer, the following look at the two graphs can understand these words:: Now the theoretical knowledge of the almost. Today, let's do a plain-style example to enhance the familiarity with table view. 1, new project new single View application, named Tableviewdemo, the development environment is: Xcode 4.3,iphone 5.1 simulator. 2. Table View put controlsOpen the Viewcontroller.xib file and drag a table view control to the existing view on the Viewcontroller.xib interface to align. 3. Connect the newly added tableview and Viewcontroller. Select the newly added TableView control, open the Connection Checker (Connection Inspector), locate the delegate and DataSource, and click on the circle cable to connect to the left file ' s owner icon, why do you want to put the two connected file ' s What about the owner? This is because the MVC design pattern used by iOS, the correspondence between the view and the Viewcontroller, requires a bridge to connect (that is, for a view, how he knows whose interface the operation should be answered by), the bridge is file's Owner. 4. Open ViewController.h, add Protocol and property (similar to Java implementation interface) [CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller<uitableviewdelegate, uitableviewdatasource>
    3. @property (Strong, nonatomic) Nsarray *list;
    4. @end

5. Open a. m file and add: [CPP]View Plaincopy
    1. @synthesize list = _list;

This is found to have two warnings that prompt for an unfinished implementation, which hints at uitableviewdelegate, uitableviewdatasource the method of the protocol in this two header file is not implemented. We'll make it happen later. 6. Establish data [CPP]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. additional setup after loading the view, typically from a nib.
  5. Nsarray *array = [[Nsarray alloc] initwithobjects:@"United States", @"Philippines",
  6. @ "Huangyan Island", @ "China", @"Thailand", @"Vietnam", @"Laos",
  7. @"Japan", nil];
  8. self.list = array;
  9. }
  10. -(void) Viewdidunload
  11. {
  12. [Super Viewdidunload];
  13. //Release any retained subviews of the main view.
  14. Self.list = nil;
  15. }

7. Generate RowThe key step is to implement TableView to add a data source, return the number of rows TableView, and return each row of cell instances. [CPP]View Plaincopy
  1. -(UITableViewCell *) TableView: (UITableView *) TableView
  2. Cellforrowatindexpath: (Nsindexpath *) Indexpath {
  3. static NSString *tablesampleidentifier = @"Tablesampleidentifier";
  4. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:
  5. Tablesampleidentifier];
  6. if (cell = = nil) {
  7. cell = [[UITableViewCell alloc]
  8. Initwithstyle:uitableviewcellstyledefault
  9. Reuseidentifier:tablesampleidentifier];
  10. }
  11. Nsuinteger row = [Indexpath row];
  12. Cell.textLabel.text = [Self.list objectatindex:row];
  13. return cell;
  14. }
In the second method above,
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:tablesampleidentifier];
This statement finds the currently reusable UITableViewCell based on the identifier tablesampleidentifier. When a row slides out of the current visible area, we reuse its corresponding UITableViewCell object, then we can save memory and resources. Here Uitableviewcellstyledefault is a constant that represents the UITableViewCell style, and in addition, there are other styles that will be used later.
Note the parameter (Nsindexpath *) Indexpath, which merges the row number row and part number section, by [Indexpath Row], and gets the line number. Then set its text to cell:
Cell.textLabel.text = [Self.list objectatindex:row]; 8, now a simple tableview to look good, run to see the effect、、 9. Add a picture. Add files to the project on the project, submit two small pictures, and then include the following code before the cell returns [CPP]View Plaincopy
    1. Nsuinteger row = [Indexpath row];
    2. Cell.textLabel.text = [Self.list objectatindex:row];
    3. UIImage *image = [UIImage imagenamed:@"QQ"];
    4. Cell.imageView.image = image;
    5. UIImage *highlighedimage = [UIImage imagenamed:@"Youdao"];
    6. Cell.imageView.highlightedImage = Highlighedimage;
    7. return cell;

The effect is as follows: 10, set the style of the lineThe constants that represent UITableViewCell styles are:

Uitableviewcellstyledefault
Uitableviewcellstylesubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2 can be modified to see the effect. You can add a detail

Cell.detailtextlabel. Text =@ "Dozen and dozen ";

return cell;

11. Select a row in tableWritten before the. m file @end-(void) table will automatically prompt you to implement the method, we choose this method

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

Check to make a hint that the message is selected and the code is implemented as follows:

[CPP]View Plaincopy
    1. -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{
    2. NSString *rowstring = [self.list objectatindex:[indexpath row]];
    3. Uialertview * alter = [[Uialertview alloc] initwithtitle:@"Selected line information" Message:rowstring delegate:self  cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
    4. [Alter show];
    5. }

Effect:

The above is the plain style TableView example code: http://download.csdn.net/detail/totogo2010/4361870

Simple use of the table view for iOS learning

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.