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 Table component to implement single list in Swift

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

Share

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

This article is to share with you about Swift how to use table components to achieve a single list, the editor feels very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

1. Sample description:

(1) the contents of the list are read from the Controls.plist file with the type Array.

(2) clicking on the list item will pop up a message box to display the information.

(3) hold down the list item and slide to the left, and the delete button appears. Click Delete to delete the item.

2. Effect drawing

3. Cell reuse mechanism

Because the cell form of the pair in the ordinary table view is generally the same, this example uses the cell reuse mechanism, which can greatly improve the program performance.

This is done by initializing the creation of a UITableView instance using registerClass (UITableViewCell.self, forCellReuseIdentifier: "SwiftCell") to create a reusable UITableViewCell. And register it with UITableView,ID as SwiftCell.

The next time you encounter a unit of the same form (or structure), you can directly use UITableView's dequeueReusableCellWithIdentifier method to extract it from the UITableView.

4. Sample code

-ViewController.swift

Import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {var ctrlnames: [String]? Var tableView: UITableView? Override func loadView () {super .loadView ()} override func viewDidLoad () {super .viewDidLoad () / initialize the data, this time we put self .ctrlnames = NSArray (contentsOfFile: NSBundle .mainBundle (). PathForResource ("Controls", ofType: "plist")!) in the property list file. As? Array print (self. Ctrlnames) / / create table view self. TableView = UITableView (frame: self. View.frame, style: UITableViewStyle. Plain) self .tableView! .delegate = self self .tableView! .DataSource = self / / create a reused cell self. TableView! .registerClass (UITableViewCell. Self, forCellReuseIdentifier: "SwiftCell") self .view.addSubview (self .tableView!) / create the header tag let headerLabel = UILabel (frame: CGRectMake (0,0, self .views.size.width, 30)) 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) self .tableView! .tableHeaderView = headerLabel} / / in this case, there is only one partition func numberOfSectionsInTableView (tableView: UITableView)-> Int {return 1 } / / returns the number of table rows (that is, the number of controls returned) func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {return self .ctrlnames! .count} / / create the display content of each cell (create the cell specified by the parameter indexPath) func tableView (tableView: UITableView) CellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {/ / to provide table display performance The cells that have been created need to be reused with let identify: String = "SwiftCell" / / cells of the same form, and let cell = tableView.dequeueReusableCellWithIdentifier (identify, forIndexPath: indexPath) as UITableViewCell cell.accessoryType = UITableViewCellAccessoryType is registered at the time of declaration. DisclosureIndicator cell.textLabel?.text = self .ctrlnames! [indexPath.row] return cell} / / UITableViewDelegate method Handle the selected event func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {self .tableView! .deselectRowAtIndexPath (indexPath, animated: true) let itemString = self .ctrlnames! [indexPath.row] let alertController = UIAlertController (title: "hint!", message: "you selected [(itemString)]", preferredStyle:. Alert) let okAction = UIAlertAction (title: "OK", style:. Default, handler: nil) alertController.addAction (okAction) self. Slide ViewController (alertController, animated: true, completion: nil)} / / method func tableView (tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle) that must be implemented for sliding deletion ForRowAtIndexPath indexPath: NSIndexPath) {print ("delete\ (indexPath.row)") let index = indexPath.row self .ctrlnames? .removeAtIndex (index) self .tableView? .deleteRowsAtIndexPaths ([indexPath], withRowAnimation: UITableViewRowAnimation. Top)} / / sliding delete func tableView (tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCellEditingStyle {return UITableViewCellEditingStyle. Delete} / / modify the text of the delete button func tableView (tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath)-> String? {return "delete"} override func didReceiveMemoryWarning () {super .didReceiveMemoryWarning ()}} above is how Swift uses the table component to implement a single list The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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