Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use swift Table Control

Shulou Source: shulou.com Published: 2022-06-01 20:35:34 10月15日 Update

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

The details are as follows

1. Effect drawing

A, ViewController inherits not only from UIViewController, but also from UITableViewDelegate,UITableViewDataSource.

B. there are three ways to re-implement UITableView by yourself. They are: numberOfSectionInTableView (table:UITableView), tableView (table:UITableView, numberOfRowsInSetion section:Int), tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

3. The version of Xcode I use: 7.2,

4. Complete source code

Import UIKitclass ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {let ctrlName = ["button", "label", "text box", "prompt box", "switch button"]; var tableView:UITableView?; override func loadView () {super.loadView () } override func viewDidLoad () {super.viewDidLoad () / / create the view tableView = UITableView (frame: view.frame, style: .Plain); tableViewroom.dataSource = self; tableViewroom.delegate = self; tableView?.registerClass (UITableViewCell.self, forCellReuseIdentifier: "SwiftCell"); view.addSubview (tableView!) / / create the tag let headerLabel = UILabel (frame: CGRectMake (0,0, view.bounds.size .width, 30)); / / set the tag attribute headerLabel.backgroundColor = UIColor.blackColor (); headerLabel.textColor = UIColor.whiteColor (); headerLabel.numberOfLines = 0; headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping HeaderLabel.text = "Common UIKIt controls"; headerLabel.font = UIFont.italicSystemFontOfSize (20); / / add the label tableViewview. TableHeaderView = headerLabel;} / set the partition func numberOfSectionsInTableView (tableView: UITableView)-> Int {return 1 } / / return the number of table rows func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {return ctrlName.count } / / create cell display content, (create cells specified by indexPath) func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {/ in order to improve table display performance, reuse let identify = "SwiftCell" for the created cells / / cells of the same form are reused, and let cell = tableView.dequeueReusableCellWithIdentifier (identify, forIndexPath: indexPath) is registered at the time of declaration; cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator; cell.textLabel?.text = ctrlName [indexPath.row]; return cell } / / UITableViewDelegate method to handle the list item selection event func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {tableView.deselectRowAtIndexPath (indexPath, animated: true); let itemString = ctrlName [indexPath.row]; / / create a prompt box let alertView = UIAlertController (title: "prompt", message: "you selected\ (itemString)", preferredStyle: .Alert) / / add button let alertAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.Default, handler: {(action)-> Void in}); alertView.addAction (alertAction); presentViewController (alertView, animated:true, completion:nil);} override func didReceiveMemoryWarning () {super.didReceiveMemoryWarning () / / Dispose of any resources that can be recreated. }} this is the end of this article on "how to use swift Table controls". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Controls tables cells prompts content buttons labels articles methods more good practical events code places properties common form performance effects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Apple MariaDB Docker