{"id":595,"date":"2023-02-25T20:01:37","date_gmt":"2023-02-25T12:01:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=595"},"modified":"2023-02-26T09:59:55","modified_gmt":"2023-02-26T01:59:55","slug":"swift-ui-add-pictures-and-detailed-text-labels-to-uitableviewcell","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-ui-add-pictures-and-detailed-text-labels-to-uitableviewcell\/","title":{"rendered":"Swift UI &#8211; \u7ed9\u8868\u683c\u7684\u5355\u5143\u683cUITableViewCell\u6dfb\u52a0\u56fe\u7247\uff0c\u8be6\u7ec6\u6587\u672c\u6807\u7b7e"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<p>\u8868\u683c<code>UITableView<\/code>\u4e2d\uff0c\u6bcf\u4e00\u5355\u5143\u683c\u90fd\u662f\u4e00\u4e2a<code>UITableViewCell<\/code>\u3002\u5176\u652f\u6301\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\uff0c\u6bd4\u5982\u5728\u5355\u5143\u683c\u7684\u5185\u90e8\uff0c\u6dfb\u52a0\u56fe\u7247\u548c\u8be6\u7ec6\u6587\u672c\u6807\u7b7e\u3002<\/p>\n<p><strong>\u6ce8\u610f<code>UITableViewCell<\/code>\u7684style<\/strong>\uff1a<\/p>\n<p><!-- more --><\/p>\n<p>\uff081\uff09<code>UITableViewCellStyle.default<\/code>\uff1a\u9ed8\u8ba4\u7684\uff0c\u53ea\u6709\u4e00\u4e2a\u5e38\u89c4\u5185\u5bb9\u6807\u7b7e\u548c\u4e00\u4e2a\u53ef\u9009\u7684<code>UIImageView<\/code><br \/>\n\uff082\uff09<code>UITableViewCellStyle.value1<\/code>\uff1a\u5185\u5bb9\u6807\u7b7e\u5728\u5de6\uff0c\u8be6\u7ec6\u6807\u7b7e\u5728\u53f3\uff0c\u53f3\u8fb9\u662f\u84dd\u8272\u6216\u7070\u8272\u7684\u6587\u672c<br \/>\n\uff083\uff09<code>UITableViewCellStyle.value2<\/code>\uff1a\u540c Value1 \u4f4d\u7f6e\u76f8\u540c\uff0c\u5de6\u8fb9\u662f\u84dd\u8272\u6587\u672c<br \/>\n\uff084\uff09<code>UITableViewCellStyle.subtitle<\/code>\uff1a\u6807\u7b7e\u4e0a\u4e0b\u653e\u7f6e<\/p>\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, 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 = NSLineBreakMode.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        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) -&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\n        \/\/\u540c\u4e00\u5f62\u5f0f\u7684\u5355\u5143\u683c\u91cd\u590d\u4f7f\u7528\uff0c\u5728\u58f0\u660e\u65f6\u5df2\u6ce8\u518c\n        let sectionNo = indexPath.section\n        let data = self.allNames?[sectionNo]\n        if(sectionNo == 0) {\n            let cell = tableView.dequeueReusableCell(withIdentifier: identify,\n                                    for: indexPath as IndexPath) as UITableViewCell\n            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator\n\n            let image = UIImage(named:&quot;heart.png&quot;)\n            cell.imageView?.image = image\n            cell.textLabel?.text = data![indexPath.row]\n\n            return cell\n        } else {\n            \/\/\u7b2c\u4e8c\u4e2a\u5206\u7ec4\u8868\u683c\u4f7f\u7528\u8be6\u7ec6\u6807\u7b7e\n            let adcell = UITableViewCell(style: UITableViewCellStyle.subtitle,\n                                         reuseIdentifier: &quot;SwiftCell&quot;)\n            adcell.textLabel?.text = data![indexPath.row]\n            print(adcell.textLabel!.text ?? &quot;&quot;)\n            adcell.detailTextLabel!.text = &quot;\u8fd9\u662f\\(data![indexPath.row])\u7684\u8bf4\u660e&quot;\n\n            return adcell\n        }\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 as 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 okAction = UIAlertAction(title: &quot;\u786e\u5b9a&quot;, style: .cancel, handler: nil)\n        alertController.addAction(okAction)\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 \u8868\u683cUITableView\u4e2d\uff0c\u6bcf\u4e00\u5355\u5143\u683c\u90fd\u662f\u4e00\u4e2aUITableViewCell\u3002 [&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-595","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/595","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=595"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/595\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}