{"id":594,"date":"2023-02-25T20:00:34","date_gmt":"2023-02-25T12:00:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=594"},"modified":"2023-02-26T10:01:13","modified_gmt":"2023-02-26T02:01:13","slug":"swift-ui-use-uitableview-to-implement-grouping-list","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-use-uitableview-to-implement-grouping-list\/","title":{"rendered":"Swift UI &#8211; \u4f7f\u7528\u8868\u683c\u7ec4\u4ef6\uff08UITableView\uff09\u5b9e\u73b0\u5206\u7ec4\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\u4ee5\u5206\u7ec4\u7684\u5f62\u5f0f\u5c55\u793a<br \/>\n\uff082\uff09\u540c\u65f6\u8fd8\u81ea\u5b9a\u4e49\u5206\u533a\u7684\u5934\u90e8\u548c\u5c3e\u90e8<br \/>\n\uff083\uff09\u70b9\u51fb\u5217\u8868\u9879\u4f1a\u5f39\u51fa\u6d88\u606f\u6846\u663e\u793a\u8be5\u9879\u4fe1\u606f<\/p>\n<p><!-- more --><\/p>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource {\n\n    var tableView:UITableView?\n\n    var allNames:Dictionary&lt;Int, [String]&gt;?\n\n    var adHeaders:[String]?\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.allNames =  [\n            0:[String]([\n                &quot;UILabel \u6807\u7b7e&quot;,\n                &quot;UITextField \u6587\u672c\u6846&quot;,\n                &quot;UIButton \u6309\u94ae&quot;]),\n            1:[String]([\n                &quot;UIDatePiker \u65e5\u671f\u9009\u62e9\u5668&quot;,\n                &quot;UIToolbar \u5de5\u5177\u6761&quot;,\n                &quot;UITableView \u8868\u683c\u89c6\u56fe&quot;])\n        ];\n\n        print(self.allNames)\n\n        self.adHeaders = [\n            &quot;\u5e38\u89c1 UIKit \u63a7\u4ef6&quot;,\n            &quot;\u9ad8\u7ea7 UIKit \u63a7\u4ef6&quot;\n        ]\n\n        \/\/\u521b\u5efa\u8868\u89c6\u56fe\n        self.tableView = UITableView(frame:self.view.frame, style:.grouped)\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 headerLabel = UILabel(frame: CGRect(x:0, y:0,\n                                width:self.view.bounds.size.width, height:30))\n        headerLabel.backgroundColor = UIColor.black\n        headerLabel.textColor = UIColor.white\n        headerLabel.numberOfLines = 0\n        headerLabel.lineBreakMode = .byWordWrapping\n        headerLabel.text = &quot;\u9ad8\u7ea7 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\u67092\u4e2a\u5206\u533a\n    func numberOfSections(in tableView: UITableView) -&gt; Int {\n        return 2;\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        let data = self.allNames?[section]\n        return data!.count\n    }\n\n    \/\/UITableViewDataSource\u534f\u8bae\u4e2d\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u51b3\u5b9a\u6307\u5b9a\u5206\u533a\u7684\u5934\u90e8\n    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {\n        return self.adHeaders?[section]\n    }\n\n    \/\/UITableViewDataSource\u534f\u8bae\u4e2d\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u51b3\u5b9a\u6307\u5b9a\u5206\u533a\u7684\u5c3e\u90e8\n    func tableView(_ tableView: UITableView, titleForFooterInSection section:Int) -&gt; String? {\n        let data = self.allNames?[section]\n        return &quot;\u6709\\(data!.count)\u4e2a\u63a7\u4ef6&quot;\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, for: indexPath)\n            cell.accessoryType = .disclosureIndicator\n\n            let sectionNo = indexPath.section\n            var data = self.allNames?[sectionNo]\n            cell.textLabel?.text = data![indexPath.row]\n\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        let itemString = self.allNames![indexPath.section]![indexPath.row]\n        let alertController = UIAlertController(title: &quot;\u63d0\u793a!&quot;,\n                                                message: &quot;\u4f60\u9009\u4e2d\u4e86\u3010\\(itemString)\u3011&quot;,\n                                                preferredStyle: .alert)\n        let cancelAction = UIAlertAction(title: &quot;\u786e\u5b9a&quot;, style: .cancel, handler: nil)\n        alertController.addAction(cancelAction)\n        self.present(alertController, animated: true, completion: nil)\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u9700\u6c42\u8bf4\u660e \uff081\uff09\u5217\u8868\u4ee5\u5206\u7ec4\u7684\u5f62\u5f0f\u5c55\u793a \uff082\uff09\u540c\u65f6\u8fd8\u81ea\u5b9a\u4e49\u5206\u533a\u7684\u5934\u90e8\u548c\u5c3e\u90e8 \uff083\uff09 [&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-594","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/594","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=594"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}