IOS project development practice-learn to use the TableView list control (2)

Source: Internet
Author: User

IOS project development practice-learn to use the TableView list control (2)

To use the TableView list control in iOS development, you can not only directly use TableViewController as the main interface, but also use the TableView control. TableView can be used for more customization to meet more requirements. It is commonly used in development. The specific implementation is as follows:

(1) create a Single View Controller project, select Swift as the language, and drag a TableView control in Main. storyboard. In this case, it seems that the entire design interface is the same as that created by TableViewController.

Then select 1 in Prototype Cells on the right. We use this cell for design. Drag a Label in the Cell and set the tag of the Label to 101. Of course, you can set the tag value by yourself. The tag values of different controls in the cell are different.

(2) input the Class value of this interface into ViewController.

(3) In the code, ViewController implements a Protocol, UITableViewDataSource, and the following two methods must be implemented.

 

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int             func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

(4) Note: In this step, you can easily forget to bind the current TableView control to the DataSource. There are two methods for this step:

 

1] bind the TableView control to the code and implement self. DataSource = self

2] Simple Method: Right-click TableView on the storyboard and drag it to ViewController. dataSource and delegate. Select dataSource.

 

(5) set the ID of the Prototype Cells just generated. Any string can be used. I set it to cell.

(6) implement the following in the Code:

 

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    return 3  }  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier(cell) as! UITableViewCell        var title = cell.viewWithTag(101) as! UILabel    title.text = Hello ,Swift        return cell      }

(7) run the program with the following effects:

 

.

 

(8) but is there any way to set different texts in cells? This is acceptable. I only need to declare an array and then read different cells. The implementation code is as follows:

 

import UIKitclass ViewController: UIViewController ,UITableViewDataSource{    var array:[String] = [Hello,iOS,Swift]    override func viewDidLoad() {    super.viewDidLoad()    // Do any additional setup after loading the view, typically from a nib.      }  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    return 3  }  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier(cell) as! UITableViewCell        var title = cell.viewWithTag(101) as! UILabel    title.text = array[indexPath.row]        return cell      }}

(9) The implementation effect is as follows:

 

 

 

 

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.