{"id":593,"date":"2023-02-25T19:59:48","date_gmt":"2023-02-25T11:59:48","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=593"},"modified":"2023-04-29T20:29:11","modified_gmt":"2023-04-29T12:29:11","slug":"swift-ui-use-table-component-uitableview-to-implement-single-list","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-use-table-component-uitableview-to-implement-single-list\/","title":{"rendered":"Swift UI &#8211; \u4f7f\u7528\u8868\u683c\u7ec4\u4ef6\uff08UITableView\uff09\u5b9e\u73b0\u5355\u5217\u8868"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>\u9700\u6c42\u8bf4\u660e<\/h3>\n<p>\uff081\uff09\u5217\u8868\u5185\u5bb9\u4ece Controls.plist \u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u7c7b\u578b\u4e3a Array<br \/>\n\uff082\uff09\u70b9\u51fb\u5217\u8868\u9879\u4f1a\u5f39\u51fa\u6d88\u606f\u6846\u663e\u793a\u8be5\u9879\u4fe1\u606f<br \/>\n\uff083\uff09\u6309\u4f4f\u5217\u8868\u9879\u5411\u5de6\u6ed1\u52a8\uff0c\u4f1a\u51fa\u73b0\u5220\u9664\u6309\u94ae\u3002\u70b9\u51fb\u5220\u9664\u5373\u53ef\u5220\u9664\u8be5\u9879<\/p>\n<p><!-- more --><\/p>\n<h3>\u5355\u5143\u683c\u590d\u7528\u673a\u5236<\/h3>\n<p>\u7531\u4e8e\u666e\u901a\u7684\u8868\u683c\u89c6\u56fe\u4e2d\u5355\u5143\u683c\u5f62\u5f0f\u4e00\u822c\u90fd\u662f\u76f8\u540c\u7684\uff0c\u6240\u4ee5\u672c\u4f8b\u91c7\u7528\u4e86\u5355\u5143\u683c\u590d\u7528\u673a\u5236\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<p>\u5b9e\u73b0\u65b9\u5f0f\u662f\u521d\u59cb\u5316\u521b\u5efa <code>UITableView<\/code> \u5b9e\u4f8b\u65f6\u4f7f\u7528 <code>.register(UITableViewCell.self, forCellReuseIdentifier: &quot;SwiftCell&quot;)<\/code> \u521b\u5efa\u4e00\u4e2a\u53ef\u4f9b\u91cd\u7528\u7684 <code>UITableViewCell<\/code>\u3002\u5e76\u5c06\u5176\u6ce8\u518c\u5230<code>UITableView<\/code>\uff0cID\u4e3a <code>SwiftCell<\/code>\u3002\u4e0b\u6b21\u78b0\u5230\u5f62\u5f0f\uff08\u6216\u7ed3\u6784\uff09\u76f8\u540c\u7684\u5355\u5143\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>UITableView<\/code>\u7684<code>dequeueReusableCell<\/code>\u65b9\u6cd5\u4ece<code>UITableView<\/code>\u4e2d\u53d6\u51fa\u3002<\/p>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<p>ViewController.swift<\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {\n\n    var ctrlnames:[String]?\n    var tableView:UITableView?\n\n    override func loadView() {\n        super.loadView()\n    }\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        \/\/\u521d\u59cb\u5316\u6570\u636e\uff0c\u8fd9\u4e00\u6b21\u6570\u636e\uff0c\u6211\u4eec\u653e\u5728\u5c5e\u6027\u5217\u8868\u6587\u4ef6\u91cc\n        self.ctrlNames = NSArray(contentsOfFile:\n            Bundle.main.path(forResource: &quot;Controls&quot;, ofType:&quot;plist&quot;)!) as? Array\n\n        print(self.ctrlNames)\n\n        \/\/\u521b\u5efa\u8868\u89c6\u56fe\n        self.tableView = UITableView(frame: self.view.frame, style:.plain)\n        self.tableView!.delegate = self\n        self.tableView!.dataSource = self\n        \/\/\u521b\u5efa\u4e00\u4e2a\u91cd\u7528\u7684\u5355\u5143\u683c\n        self.tableView!.register(UITableViewCell.self,\n                                forCellReuseIdentifier: &quot;SwiftCell&quot;)\n        self.view.addSubview(self.tableView!)\n\n        \/\/\u521b\u5efa\u8868\u5934\u6807\u7b7e\n        let frame = CGRect(x:0, y:0, width:self.view.bounds.size.width, height:30)\n        let headerLabel = UILabel(frame: frame)\n        headerLabel.backgroundColor = UIColor.black\n        headerLabel.textColor = UIColor.white\n        headerLabel.numberOfLines = 0\n        headerLabel.lineBreakMode = .byWordWrapping\n        headerLabel.text = &quot;\u5e38\u89c1 UIKit \u63a7\u4ef6&quot;\n        headerLabel.font = UIFont.italicSystemFont(ofSize: 20)\n        self.tableView!.tableHeaderView = headerLabel\n    }\n\n    \/\/\u8fd4\u56de\u8868\u683c\u5206\u533a\u6570\uff0c\u672c\u4f8b\u53ea\u6709\u4e00\u4e2a\u5206\u533a\n    func numberOfSections(in tableView: UITableView) -&gt; Int {\n        return 1\n    }\n\n    \/\/\u8fd4\u56de\u8868\u683c\u884c\u6570\uff08\u4e5f\u5c31\u662f\u8fd4\u56de\u63a7\u4ef6\u6570\uff09\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {\n        return self.ctrlNames!.count\n    }\n\n    \/\/\u521b\u5efa\u5404\u5355\u5143\u663e\u793a\u5185\u5bb9(\u521b\u5efa\u53c2\u6570indexPath\u6307\u5b9a\u7684\u5355\u5143\uff09\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)\n        -&gt; UITableViewCell {\n        \/\/\u4e3a\u4e86\u63d0\u4f9b\u8868\u683c\u663e\u793a\u6027\u80fd\uff0c\u5df2\u521b\u5efa\u5b8c\u6210\u7684\u5355\u5143\u9700\u91cd\u590d\u4f7f\u7528\n        let identify:String = &quot;SwiftCell&quot;\n        \/\/\u540c\u4e00\u5f62\u5f0f\u7684\u5355\u5143\u683c\u91cd\u590d\u4f7f\u7528\uff0c\u5728\u58f0\u660e\u65f6\u5df2\u6ce8\u518c\n        let cell = tableView.dequeueReusableCell(withIdentifier: identify,\n                                                for: indexPath)\n        cell.accessoryType = .disclosureIndicator\n        cell.textLabel?.text = self.ctrlNames![indexPath.row]\n        return cell\n    }\n\n    \/\/ UITableViewDelegate \u65b9\u6cd5\uff0c\u5904\u7406\u5217\u8868\u9879\u7684\u9009\u4e2d\u4e8b\u4ef6\n    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {\n        self.tableView!.deselectRow(at: indexPath, animated: true)\n\n        let itemString = self.ctrlNames![indexPath.row]\n\n        let alertController = UIAlertController(title: &quot;\u63d0\u793a!&quot;,\n                                message: &quot;\u4f60\u9009\u4e2d\u4e86\u3010\\(itemString)\u3011&quot;, preferredStyle: .alert)\n        let okAction = UIAlertAction(title: &quot;\u786e\u5b9a&quot;, style: .default, handler: nil)\n        alertController.addAction(okAction)\n        self.present(alertController, animated: true, completion: nil)\n    }\n\n    \/\/\u6ed1\u52a8\u5220\u9664\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5\n    func tableView(_ tableView: UITableView,\n                   commit editingStyle: UITableViewCellEditingStyle,\n                   forRowAt indexPath: IndexPath) {\n        print(&quot;\u5220\u9664\\(indexPath.row)&quot;)\n        let index = indexPath.row\n        self.ctrlNames?.remove(at: index)\n        self.tableView?.deleteRows(at: [indexPath], with: .top)\n    }\n\n    \/\/\u6ed1\u52a8\u5220\u9664\n    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath)\n        -&gt; UITableViewCellEditingStyle {\n        return UITableViewCellEditingStyle.delete\n    }\n\n    \/\/\u4fee\u6539\u5220\u9664\u6309\u94ae\u7684\u6587\u5b57\n    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -&gt; String? {\n        return &quot;\u5220\u9664&quot;\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/code><\/pre>\n<p>Controls.plist<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE plist PUBLIC &quot;-\/\/Apple\/\/DTD PLIST 1.0\/\/EN&quot;\n&quot;http:\/\/www.apple.com\/DTDs\/PropertyList-1.0.dtd&quot;&gt;\n&lt;plist version=&quot;1.0&quot;&gt;\n&lt;array&gt;\n    &lt;string&gt;\u6807\u7b7e UILabel&lt;\/string&gt;\n    &lt;string&gt;\u6587\u672c\u6846  UITextField&lt;\/string&gt;\n    &lt;string&gt;\u6309\u94ae UIButton&lt;\/string&gt;\n    &lt;string&gt;\u5f00\u5173\u6309\u94ae UISwitch&lt;\/string&gt;\n    &lt;string&gt;\u5206\u6bb5\u63a7\u4ef6 UISegmentControl&lt;\/string&gt;\n    &lt;string&gt;\u56fe\u50cf UIImageView&lt;\/string&gt;\n    &lt;string&gt;\u8fdb\u5ea6\u6761 UIProgressView&lt;\/string&gt;\n    &lt;string&gt;\u6ed1\u52a8\u6761 UISlider&lt;\/string&gt;\n    &lt;string&gt;\u8b66\u544a\u6846 UIAlertView&lt;\/string&gt;\n&lt;\/array&gt;\n&lt;\/plist&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u9700\u6c42\u8bf4\u660e \uff081\uff09\u5217\u8868\u5185\u5bb9\u4ece Controls.plist \u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u7c7b\u578b\u4e3a A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}