Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use tableView in Swift

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use tableView in Swift. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

/ / the way to abide by the agreement, which is directly followed by the inherited parent class, and the + protocol is fine.

Class ViewController: UIViewController {

Override func viewDidLoad () {

Super.viewDidLoad ()

/ / add controls for tableView

Let tableView = UITableView ()

TableView.frame = self.view.bounds

Self.view.addSubview (tableView)

/ / set data source, set data

TableView.dataSource = self

TableView.delegate = self

}

}

/ / equivalent to category in OC

Extension ViewController: UITableViewDataSource

{

/ / the method of realizing data source by MARK:-

Func numberOfSectionsInTableView (tableView: UITableView)-> Int {

Return 1

}

Func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {

Return 20

}

Func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {

Let ID: String = "Cell"

Var cell = tableView.dequeueReusableCellWithIdentifier (ID)

If cell = = nil {

Cell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: ID)

}

Cell?.textLabel?.text = "Test data:\ (indexPath.row)"

Return cell!

}

}

Extension ViewController: UITableViewDelegate

{

/ / MARK:- implements the proxy method

Func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

Print (indexPath.row)

}

}

/ / UITableViewDataSource and UITableViewDelegate can be put together

This is the end of the article on "how to use tableView in Swift". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report