{"id":598,"date":"2023-02-25T20:03:40","date_gmt":"2023-02-25T12:03:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=598"},"modified":"2023-02-26T08:14:07","modified_gmt":"2023-02-26T00:14:07","slug":"swift-ui-add-editing-functions-to-tables","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-add-editing-functions-to-tables\/","title":{"rendered":"Swift UI &#8211; \u7ed9\u8868\u683c\u6dfb\u52a0\u7f16\u8f91\u529f\u80fd\uff08\u5220\u9664\uff0c\u63d2\u5165\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>UITableView\u63d2\u5165\u53ca\u5220\u9664<\/h3>\n<p>\uff081\uff09\u7ed9\u8868\u683c\u6dfb\u52a0\u957f\u6309\u529f\u80fd\uff0c\u957f\u6309\u540e\u8868\u683c\u8fdb\u5165\u7f16\u8f91\u72b6\u6001<br \/>\n\uff082\uff09\u5728\u7f16\u8f91\u72b6\u6001\u4e0b\uff0c\u7b2c\u4e00\u4e2a\u5206\u7ec4\u5904\u4e8e\u5220\u9664\u72b6\u6001\uff0c\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u5904\u4e8e\u63d2\u5165\u72b6\u6001<br \/>\n\uff083\uff09\u70b9\u51fb\u5220\u9664\u56fe\u6807\uff0c\u5220\u9664\u5bf9\u5e94\u6761\u76ee<br \/>\n\uff084\uff09\u70b9\u51fb\u6dfb\u52a0\u56fe\u6807\uff0c\u63d2\u5165\u4e00\u6761\u65b0\u6570\u636e<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">import UIKit\n\nclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {\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;UIButton \u6309\u94ae&quot;]),\n            1:[String]([\n                &quot;UIDatePiker \u65e5\u671f\u9009\u62e9\u5668&quot;,\n                &quot;UITableView \u8868\u683c\u89c6\u56fe&quot;])\n        ];\n\n        print(self.allNames ?? &quot;&quot;)\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,\n                                                height:30))\n        headerLabel.backgroundColor = UIColor.black\n        headerLabel.textColor = UIColor.white\n        headerLabel.numberOfLines = 0\n        headerLabel.lineBreakMode = NSLineBreakMode.byWordWrapping\n        headerLabel.text = &quot;UIKit \u63a7\u4ef6&quot;\n        headerLabel.font = UIFont.italicSystemFont(ofSize: 20)\n        self.tableView!.tableHeaderView = headerLabel\n\n        \/\/\u7ed1\u5b9a\u5bf9\u957f\u6309\u7684\u54cd\u5e94\n        let longPress = UILongPressGestureRecognizer(target:self,\n                        action:#selector(ViewController.tableviewCellLongPressed(_:)))\n        \/\/\u4ee3\u7406\n        longPress.delegate = self\n        longPress.minimumPressDuration = 1.0\n        \/\/\u5c06\u957f\u6309\u624b\u52bf\u6dfb\u52a0\u5230\u9700\u8981\u5b9e\u73b0\u957f\u6309\u64cd\u4f5c\u7684\u89c6\u56fe\u91cc\n        self.tableView!.addGestureRecognizer(longPress)\n    }\n\n    \/\/\u5355\u5143\u683c\u957f\u6309\u4e8b\u4ef6\u54cd\u5e94\n    @objc func tableviewCellLongPressed(_ gestureRecognizer:UILongPressGestureRecognizer)\n    {\n        if (gestureRecognizer.state == UIGestureRecognizerState.began)\n        {\n            print(&quot;UIGestureRecognizerStateBegan&quot;);\n        }\n        if (gestureRecognizer.state == UIGestureRecognizerState.changed)\n        {\n            print(&quot;UIGestureRecognizerStateChanged&quot;);\n        }\n\n        if (gestureRecognizer.state == UIGestureRecognizerState.ended)\n        {\n            print(&quot;UIGestureRecognizerStateEnded&quot;);\n            \/\/\u5728\u6b63\u5e38\u72b6\u6001\u548c\u7f16\u8f91\u72b6\u6001\u4e4b\u95f4\u5207\u6362\n            if(self.tableView!.isEditing == false)\n            {\n                self.tableView!.setEditing(true, animated:true)\n            }\n            else\n            {\n                self.tableView!.setEditing(false, animated:true)\n            }\n        }\n    }\n\n    \/\/\u5728\u672c\u4f8b\u4e2d\uff0c\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)\n        -&gt; String? {\n        var headers = self.adHeaders!\n        return headers[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)\n        -&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,\n                                                for: indexPath)\n        cell.accessoryType = .disclosureIndicator\n\n        let secno = indexPath.section\n        var data = self.allNames?[secno]\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\n        let itemString = self.allNames![indexPath.section]![indexPath.row]\n\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    \/\/\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u7f16\u8f91\u7684\u6837\u5f0f\n    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath)\n        -&gt; UITableViewCellEditingStyle {\n            if (indexPath.section == 1)\n            {\n                return UITableViewCellEditingStyle.insert\n            }\n            return UITableViewCellEditingStyle.delete\n    }\n\n    \/\/\u8bbe\u7f6e\u786e\u8ba4\u5220\u9664\u6309\u94ae\u7684\u6587\u5b57\n    func tableView(_ tableView: UITableView,\n                   titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -&gt; String? {\n            var data = self.allNames?[indexPath.section]!\n\n            let itemString = data![indexPath.row] as String\n            return &quot;\u786e\u5b9a\u5220\u9664\\(itemString)\uff1f&quot;\n    }\n\n    \/\/\u5355\u5143\u683c\u7f16\u8f91\u540e\uff08\u5220\u9664\u6216\u63d2\u5165\uff09\u7684\u54cd\u5e94\u65b9\u6cd5\n    func tableView(_ tableView: UITableView,\n                   commit editingStyle: UITableViewCellEditingStyle,\n                   forRowAt indexPath: IndexPath) {\n        if (editingStyle == UITableViewCellEditingStyle.delete)\n        {\n            self.allNames?[indexPath.section]?.remove(at: indexPath.row)\n\n            self.tableView!.reloadData()\n            self.tableView!.setEditing(true, animated: true)\n            print(&quot;\u4f60\u786e\u8ba4\u4e86\u5220\u9664\u6309\u94ae&quot;)\n            \/\/ Array\n        }\n        else if (editingStyle == UITableViewCellEditingStyle.insert)\n        {\n            self.allNames?[indexPath.section]?.insert(&quot;\u63d2\u5165\u4e00\u9879\u65b0\u7684&quot;,\n                                                    at: indexPath.row+1)\n            print(&quot;\u4f60\u6309\u4e0b\u4e86\u63d2\u5165\u6309\u94ae&quot;)\n            self.tableView!.reloadData()\n        }\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/code><\/pre>\n<h3>\u529f\u80fd\u6539\u8fdb\u4e00<\/h3>\n<p>\uff081\uff09\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6240\u6709\u5355\u5143\u683c\u90fd\u6709\u6ed1\u52a8\u5220\u9664\u529f\u80fd\u3002<br \/>\n\uff082\uff09\u957f\u6309\u8868\u683c\u8fdb\u5165\u7f16\u8f91\u72b6\u6001\uff0c\u6240\u6709\u5355\u5143\u683c\u53c8\u90fd\u8fdb\u5165\u63d2\u5165\u72b6\u6001\u3002\u70b9\u51fb\u6dfb\u52a0\u56fe\u6807\uff0c\u5373\u53ef\u63d2\u5165\u4e00\u6761\u65b0\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-swift\">\/\/\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u7f16\u8f91\u7684\u6837\u5f0f\nfunc tableView(_ tableView: UITableView,\n                editingStyleForRowAt indexPath: IndexPath)\n    -&gt; UITableViewCellEditingStyle {\n        if (self.tableView!.isEditing == false)\n        {\n            return UITableViewCellEditingStyle.delete\n        }\n        else\n        {\n            return UITableViewCellEditingStyle.insert\n        }\n}\n\n\/\/\u5355\u5143\u683c\u7f16\u8f91\u540e\uff08\u5220\u9664\u6216\u63d2\u5165\uff09\u7684\u54cd\u5e94\u65b9\u6cd5\nfunc tableView(_ tableView: UITableView,\n                commit editingStyle: UITableViewCellEditingStyle,\n                forRowAt indexPath: IndexPath) {\n    if(editingStyle == UITableViewCellEditingStyle.delete)\n    {\n        self.allNames?[indexPath.section]?.remove(at: indexPath.row)\n        self.tableView!.reloadData()\n        print(&quot;\u4f60\u786e\u8ba4\u4e86\u5220\u9664\u6309\u94ae&quot;)\n    }\n    else if(editingStyle == UITableViewCellEditingStyle.insert)\n    {\n        self.allNames?[indexPath.section]?.insert(&quot;\u63d2\u5165\u4e00\u9879\u65b0\u7684&quot;,\n                                                at: indexPath.row+1)\n        print(&quot;\u4f60\u6309\u4e0b\u4e86\u63d2\u5165\u6309\u94ae&quot;)\n        self.tableView!.reloadData()\n    }\n}<\/code><\/pre>\n<h3>\u529f\u80fd\u6539\u8fdb\u4e8c<\/h3>\n<p>\uff081\uff09\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6240\u6709\u5355\u5143\u683c\u90fd\u65e0\u6cd5\u8fdb\u884c\u6ed1\u52a8\u5220\u9664\u7b49\u7f16\u8f91\u64cd\u4f5c\u3002<br \/>\n\uff082\uff09\u957f\u6309\u8868\u683c\u8fdb\u5165\u7f16\u8f91\u72b6\u6001\uff0c\u6240\u6709\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002<br \/>\n\uff083\uff09\u540c\u65f6\u5728\u7f16\u8f91\u72b6\u6001\u4e0b\uff0c\u5728\u4e0b\u65b9\u4f1a\u81ea\u52a8\u51fa\u73b0\u4e00\u4e2a\u65b0\u589e\u64cd\u4f5c\u5355\u5143\u683c\u3002\u70b9\u51fb\u524d\u9762\u7684\u52a0\u53f7\uff0c\u4fbf\u4f1a\u7ed9\u6570\u636e\u96c6\u4e2d\u6dfb\u52a0\u4e00\u6761\u65b0\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-swift\">\/\/\u5355\u5143\u683c\u957f\u6309\u4e8b\u4ef6\u54cd\u5e94\n@objc func tableviewCellLongPressed(_ gestureRecognizer:UILongPressGestureRecognizer)\n{\n    if (gestureRecognizer.state == UIGestureRecognizerState.began)\n    {\n        print(&quot;UIGestureRecognizerStateBegan&quot;);\n    }\n    if (gestureRecognizer.state == UIGestureRecognizerState.changed)\n    {\n        print(&quot;UIGestureRecognizerStateChanged&quot;);\n    }\n\n    if (gestureRecognizer.state == UIGestureRecognizerState.ended)\n    {\n        print(&quot;UIGestureRecognizerStateEnded&quot;);\n        \/\/\u5728\u6b63\u5e38\u72b6\u6001\u548c\u7f16\u8f91\u72b6\u6001\u4e4b\u95f4\u5207\u6362\n        if (self.tableView!.isEditing == false)\n        {\n            self.tableView!.setEditing(true, animated:true)\n        }\n        else\n        {\n            self.tableView!.setEditing(false, animated:true)\n        }\n        self.tableView!.reloadData()\n    }\n}\n\n\/\/\u8fd4\u56de\u8868\u683c\u884c\u6570\uff08\u4e5f\u5c31\u662f\u8fd4\u56de\u63a7\u4ef6\u6570\uff09\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {\n    let data = self.allnames?[section]\n    var count = data!.count\n    if self.tableView!.isEditing {\n        count += 1\n    }\n    return count\n}\n\n\/\/\u521b\u5efa\u5404\u5355\u5143\u663e\u793a\u5185\u5bb9(\u521b\u5efa\u53c2\u6570indexPath\u6307\u5b9a\u7684\u5355\u5143\uff09\nfunc 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\n    let secno = indexPath.section\n    var data = self.allnames?[secno]\n\n    \/\/\u5224\u65ad\u662f\u5426\u662f\u65b0\u589e\u7684\u63d2\u5165\u63d0\u793a\u884c\n    if self.tableView!.isEditing &amp;&amp; indexPath.row == data!.count {\n        cell.textLabel?.text = &quot;\u6dfb\u52a0\u65b0\u6570\u636e...&quot;\n    } else {\n        cell.textLabel?.text = data![indexPath.row]\n    }\n\n    return cell\n}\n\n\/\/\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u7f16\u8f91\u7684\u6837\u5f0f\nfunc tableView(_ tableView: UITableView,\n                editingStyleForRowAt indexPath: IndexPath)\n    -&gt; UITableViewCellEditingStyle {\n    let secno = indexPath.section\n    let data = self.allnames?[secno]\n\n    if self.tableView!.isEditing == false {\n        return UITableViewCellEditingStyle.none\n    }\n    else if indexPath.row == data!.count {\n        return UITableViewCellEditingStyle.insert\n    } else {\n        return UITableViewCellEditingStyle.delete\n    }\n}\n\n\/\/\u5355\u5143\u683c\u7f16\u8f91\u540e\uff08\u5220\u9664\u6216\u63d2\u5165\uff09\u7684\u54cd\u5e94\u65b9\u6cd5\nfunc tableView(_ tableView: UITableView,\n               commit editingStyle: UITableViewCellEditingStyle,\n               forRowAt indexPath: IndexPath) {\n    if (editingStyle == UITableViewCellEditingStyle.delete)\n    {\n        self.allnames?[indexPath.section]?.remove(at: indexPath.row)\n        self.tableView!.reloadData()\n        print(&quot;\u4f60\u786e\u8ba4\u4e86\u5220\u9664\u6309\u94ae&quot;)\n    }\n    else if (editingStyle == UITableViewCellEditingStyle.insert)\n    {\n        self.allnames?[indexPath.section]?.insert(&quot;\u8fd9\u662f\u63d2\u5165\u7684\u65b0\u6570\u636e&quot;,\n                                                at: indexPath.row)\n        print(&quot;\u4f60\u6309\u4e0b\u4e86\u63d2\u5165\u6309\u94ae&quot;)\n        self.tableView!.reloadData()\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 UITableView\u63d2\u5165\u53ca\u5220\u9664 \uff081\uff09\u7ed9\u8868\u683c\u6dfb\u52a0\u957f\u6309\u529f\u80fd\uff0c\u957f\u6309\u540e\u8868\u683c\u8fdb\u5165\u7f16\u8f91\u72b6 [&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-598","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/598","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=598"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}