Simple use of Table View for iOS Learning

Source: Internet
Author: User

Table View:

In many iPhone and other iOS apps, you can see the appearance of Table View. In addition to displaying common Table data, the attribute information you set is often used in Table View, table View can be divided into the following two types:

 

  • Plain: this is a normal list Style
  • Grouped: This is the block style.
For UITableView, we have some special concepts and terminologies. For example, we define a row of Table View as Cell, while many cells can form a Section, each Section has Header and Footer at the top and bottom, and many sections form the entire Table. Of course, Table also has Header and Footer, the following two figures show the terms ::

Now the theoretical knowledge is almost understood. Let's take a Plain style example first today, so as to enhance the skillful use of Table view. 1. Create a new project and create a Single View Application named TableViewDemo. the development environment is Xcode 4.3 and iPhone 5.1 simulator. 2. Add controls to Table ViewOpen the ViewController. xib file and drag a Table View control to the ViewController. xib interface to align it with the existing View.
3. Connect the newly added TableView and ViewController.Select the newly added TableView control, open the Connection Inspector, find the delegate and datasource, and draw a circle in the dot to connect to the File's Owner icon on the left, why are these two connections on the File's Owner? This is because iOS uses the MVC design pattern, and the correspondence between views and ViewController requires a bridge to connect (that is, for a View, how does he know who should respond to the operations on his interface?) This bridge is the File's Owner.
4. Open ViewController. h and add the protocol and Property (similar to the implementation interface in java)
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>@property (strong, nonatomic) NSArray *list;@end

5. Open the. m file and add:

@synthesize list = _list;


Two Warnings are found, prompting incomplete implementations. This indicates that the Protocol methods in the UITableViewDelegate and UITableViewDataSource header files are not implemented. We will implement it later. 6. Create data

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray * array = [[NSArray alloc] initWithObjects: @ "USA", @ "Philippines", @ "Huangyan Island", @ "China", @ "Thailand ", @ "Vietnam", @ "Laos", @ "Japan", nil]; self. list = array;}-(void) viewDidUnload {[super viewDidUnload]; // Release any retained subviews of the main view. self. list = nil ;}

7. Generate rowThe key step is to add a data source to tableview, return the number of rows of TableView, and return the cell instances of each row.

- (UITableViewCell *)tableView:(UITableView *)tableView          cellForRowAtIndexPath:(NSIndexPath *)indexPath {         static NSString *TableSampleIdentifier = @"TableSampleIdentifier";         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:                              TableSampleIdentifier];     if (cell == nil) {         cell = [[UITableViewCell alloc]                 initWithStyle:UITableViewCellStyleDefault                 reuseIdentifier:TableSampleIdentifier];     }         NSUInteger row = [indexPath row];     cell.textLabel.text = [self.list objectAtIndex:row]; return cell; }
In the second method above,
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
This statement searches for reusable UITableViewCell Based on the identifier TableSampleIdentifier. When a row slides out of the current visible area, we can reuse its corresponding UITableViewCell object to save memory and resources. Here, UITableViewCellStyleDefault represents the constant of the UITableViewCell style. In addition, there are other styles that will be used later.
Note that the parameter (NSIndexPath *) indexPath combines row numbers and section numbers. You can use [indexPath row] to obtain the row number. Then set the text for the cell:
Cell. textLabel. text = [self. list objectAtIndex: row];

8. Now a simple TableView looks good. Run it to see the effect.,,9. add images.Add files to the project on the project, submit two small images, and add the following code before the cell returns

NSUInteger row = [indexPath row];     cell.textLabel.text = [self.list objectAtIndex:row];     UIImage *image = [UIImage imageNamed:@"qq"];     cell.imageView.image = image;     UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];     cell.imageView.highlightedImage = highLighedImage;return cell; 

The effect is as follows:10. Set the row styleConstants in the UITableViewCell style include:

UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
You can modify UITableViewCellStyleValue2 to see the effect. You can add a detail

Cell. detailTextLabel. text = @ "hitting ";

Return cell;

11. Select a row in the tableWrite the-(void) table before the. m file @ end. A method is automatically prompted for implementation. We will select this method.

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

Select is a prompt indicating that the information is selected. The code implementation is as follows:

 

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {NSString * rowString = [self. list objectAtIndex: [indexPath row]; UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "selected row information" message: rowString delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; [alter show];}

Effect:

 

 

The above is the Plain style TableView example code: http://download.csdn.net/detail/totogo2010/4361870 copyright statement: This article by http://blog.csdn.net/totogo2010/original, welcome to share. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!
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.