The use of iOS development UITableView

Source: Internet
Author: User

This article records the use of UITableView in iOS development, the UITableView in iOS is very similar to the ListView in Android, with a demo to illustrate:

1. New Projecttestsimpletableviewproject in Xcode

2. Drag a UITableView control into the Main.storyboard

3, in the ViewController.h file, the implementation of Uitableviewdelegate and Uitableviewdatasource protocol

What we need to say here is. To populate UITableView with data, you need to let Viewcontroller implement both protocols, similar to populating the ListView with data in Android. Need to specify a adapter for the ListView, and then rewrite some methods in the adapter, iOS is the same, the implementation of the above two protocols, you need to implement some of the two protocols, the ability to add data for UITableView.

The code for the ViewController.h file is as follows:

#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller <uitableviewdelegate, UITableView controls in the uitableviewdatasource>//interface @property (weak, nonatomic) Iboutlet UITableView *tableView;// Data in UITableView, using an array of strings to save @property (Strong, nonatomic) Nsmutablearray *tabledataarr; @end

In order to associate the TableView variable declared in ViewController.h with the UITableView control in Main.storyboard, you need to do some processing in main.storyboard, for example, as seen:


Right-click the Viewcontroller Small Circle button in the Main.storyboard interface and drag TableView from outlets to the UITableView control in the panel. This creates a link between the variable and the control.

Unlike Android, you want to connect variables to controls in a layout file. We are using the Findviewbyid method to find the appropriate control through the ID of the control.

4, the following need in the VIEWCONTROLLER.M file, processing some data, the first need to load a few test data in the Viewdidload method. The code is as follows:

-(void) viewdidload {    [super viewdidload];    Additional setup after loading the view, typically from a nib.    Load some data here. Used to display above the UITableView    [self loaddata];} #pragma mark loads some data for display on UITableView-(void) LoadData {    //initialize array    Self.tabledataarr = [Nsmutablearray array];    Add 20 strings to array for    (int i = 0; i < i++) {        [Self.tabledataarr addobject:[nsstring stringwithformat:@ "Table I Tem%i ", I]];}    }
This is the direct use of loops to generate 20 tableview data.

5, realize the Uitableviewdatasource in two methods:

-(Nsinteger) TableView: (uitableview *) TableView numberofrowsinsection: (nsinteger ) section and

-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

The code is as follows:

<span style= "FONT-SIZE:14PX;" > #pragma mark this method returns the item number in UITableView-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: ( Nsinteger) Section {    return [Self.tabledataarr count];} #pragma mark this method returns each cell in UITableView, where the data that is displayed in each cell is processed-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {    //Remove cells from queue    uitableviewcell *cell = [Self.tableview Dequeuereusablecellwithidentifier:identifier Forindexpath:indexpath];    Sets the data for the label of the cell    cell.textLabel.text = [Self.tabledataarr objectAtIndex:indexPath.row];    return cell;} </span>

The identifier here is a static variable that we define outside of the method:

Static nsstring *identifier =@ "Tableviewcell";

The function of this variable is similar to setting tag for a ListItem in Android, mainly for reusing Tableviewcell and assigning an identity to the cell, which can be found by this identifier.

6, to here the code is basically finished, but UITableView how to know where to get data from it. To specify data for the ListView in Android, you need to write an adapter adapter, then call the ListView Setadapter method to specify the data, specify the data for UITableView in iOS, In Main.storyboard, the right mouse button is placed above the UITableView. Then drag the Viewcontroller to the top of the small circle button, for example to see:



Then release the right mouse button and select DataSource in the dialog box that appears. Repeat the process above and select Delegate. This gives UITableView set up a data source and entrusted, entrusted mainly to deal with the uitableview of clicks and other events, to achieve some of the methods entrusted to deal with these events.

In addition to specifying the data source for UITableView using the above operation method. You can also set the UITableView data source directly in your code to add the following two lines of code to the Viewdidload method:

-(void) viewdidload {    [super viewdidload];    Additional setup after loading the view, typically from a nib.    Load some data here for display on UITableView    [self loaddata];    If you do not set the data source and entrust in Main.storyboard, use the following two lines of code to set the TableView data source and entrust    self.tableView.delegate = self;    Self.tableView.dataSource = self;}
7, assuming that this time directly to execute the application, will error such as the following:

Terminating app due to uncaught exception ' nsinternalinconsistencyexception ', Reason: ' Unable-dequeue a cell with ident Ifier Tableviewcell-must Register a nib or a class for the identifier or connect a prototype cell in a storyboard '

The reason is that we did not specify a cell for UITableView, and we are missing the next step:

Drag a Tableviewcell control to the UITableView above the Main.storyboard, for example, as you can see:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Then select this Tableviewcell and configure the Tableviewcell logo on the right side, for example to see:

Note that the identifier to be filled in here is consistent with the identifier that we define in the code. This ability is correctly loaded into UITableView, after the application is executed again. Results such as the following are seen:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

There should be a cutting line here, it may not be shown on the simulator, so the cutting line is not shown in.

8. To add a picture to each cell, here is a way to add a picture to project:


And then in

-(UITableViewCell *) TableView: ( UITableView *) TableView Cellforrowatindexpath: (nsindexpath *) Add the code for the settings image in the Indexpath method, as seen in the following:

#pragma mark this method returns each cell in the UITableView. Here to handle what data is displayed in each cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {    //Remove cells from the queue    uitableviewcell *cell = [Self.tableview dequeuereusablecellwithidentifier: identifier Forindexpath:indexpath];    Sets the data for the label of the cell    cell.textLabel.text = [Self.tabledataarr objectAtIndex:indexPath.row];    Set a picture for the cell    cell.imageView.image = [UIImage imagenamed:@ "Icon.png"];    return cell;}
This makes it possible to add a slice to the cell, such as what you see:



9. If you want to add a side slip delete function to the cell, you need to implement one of the methods in the protocol:

-(void) TableView: ( UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath :(nsindexpath *) Indexpath

Then add the deleted code in this method such as the following:

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {    [Self.tabledataarr removeObjectAtIndex:indexPath.row];    [Self.tableview Reloaddata];}
By adding the above code, you can remove the slider.


Use of UITableView for iOS development

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.