{"id":185,"date":"2025-05-29T10:37:15","date_gmt":"2025-06-05T03:15:14","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=185"},"modified":"2025-06-05T10:37:15","modified_gmt":"2025-06-05T03:15:14","slug":"swift-tableview-%e9%9a%a8%e8%a1%a8%e6%a0%bc%e5%85%a7%e5%ae%b9%e8%ae%8a%e9%ab%98-%e9%ab%98%e5%ba%a6%e8%87%aa%e9%81%a9%e6%87%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/uncategorized\/swift-tableview-%e9%9a%a8%e8%a1%a8%e6%a0%bc%e5%85%a7%e5%ae%b9%e8%ae%8a%e9%ab%98-%e9%ab%98%e5%ba%a6%e8%87%aa%e9%81%a9%e6%87%89\/","title":{"rendered":"Swift \u8868\u683c\u81ea\u9069\u61c9\u9ad8\u5ea6\u6559\u5b78 | 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>\u5728 Swift \u7684 TableView \u4e2d\u5be6\u73fe\u884c\u9ad8\u81ea\u9069\u61c9\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u529f\u80fd\uff0c\u7279\u5225\u662f\u5728\u986f\u793a\u52d5\u614b\u5167\u5bb9\u6642\u3002\u96a8\u8457 Swift \u8a9e\u8a00\u7684\u9032\u6b65\uff0c\u6211\u5011\u9700\u8981\u78ba\u4fdd\u4f7f\u7528\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u548c\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<p>## 1. \u8a2d\u5b9a TableView \u7684\u81ea\u9069\u61c9\u9ad8\u5ea6<\/p>\n<p>\u5728\u4f60\u7684 `UITableViewController` \u4e2d\uff0c\u9996\u5148\u9700\u8981\u8a2d\u7f6e `rowHeight` \u548c `estimatedRowHeight` \u5c6c\u6027\u3002\u9019\u662f\u8b93 TableView \u77e5\u9053\u5982\u4f55\u8a08\u7b97\u884c\u9ad8\u7684\u95dc\u9375\u6b65\u9a5f\u3002<\/p>\n<p>&#8220;`swift<br \/>\noverride func viewDidLoad() {<br \/>\n    super.viewDidLoad()<br \/>\n    tableView.rowHeight = UITableView.automaticDimension<br \/>\n    tableView.estimatedRowHeight = 100 \/\/ \u9019\u662f\u4f30\u7b97\u7684\u884c\u9ad8<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>## 2. \u5be6\u4f5c `heightForRowAt` \u65b9\u6cd5<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u70ba\u4e86\u5be6\u73fe\u884c\u9ad8\u81ea\u9069\u61c9\uff0c\u6211\u5011\u9700\u8981\u8986\u5beb `heightForRowAt` \u65b9\u6cd5\u3002\u4f7f\u7528 `UITableView.automaticDimension` \u4f86\u8b93\u7cfb\u7d71\u81ea\u52d5\u8a08\u7b97\u6bcf\u4e00\u884c\u7684\u9ad8\u5ea6\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {<br \/>\n    return UITableView.automaticDimension<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>## 3. \u78ba\u4fdd\u4f60\u7684 Cell \u652f\u6301\u81ea\u9069\u61c9<\/p>\n<p>\u5728\u4f60\u7684\u81ea\u5b9a\u7fa9 UITableViewCell \u4e2d\uff0c\u78ba\u4fdd\u6240\u6709\u9700\u8981\u81ea\u9069\u61c9\u7684 UI \u5143\u7d20\u90fd\u6b63\u78ba\u8a2d\u7f6e\u4e86 Auto Layout \u7d04\u675f\u3002\u9019\u6a23\u624d\u80fd\u78ba\u4fdd\u5728\u5167\u5bb9\u8b8a\u5316\u6642\uff0c\u884c\u9ad8\u53ef\u4ee5\u81ea\u52d5\u8abf\u6574\u3002<\/p>\n<p>&#8220;`swift<br \/>\nclass MyCustomCell: UITableViewCell {<br \/>\n    @IBOutlet weak var myLabel: UILabel!<\/p>\n<p>    override func awakeFromNib() {<br \/>\n        super.awakeFromNib()<br \/>\n        myLabel.numberOfLines = 0 \/\/ \u8a2d\u7f6e\u70ba 0 \u4ee5\u5141\u8a31\u591a\u884c\u986f\u793a<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>## 4. \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5982\u679c\u4f60\u767c\u73fe\u884c\u9ad8\u6c92\u6709\u5982\u9810\u671f\u81ea\u9069\u61c9\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd\u6240\u6709 UI \u5143\u7d20\u90fd\u6709\u6b63\u78ba\u7684 Auto Layout \u7d04\u675f\u3002<br \/>\n&#8211; \u78ba\u4fdd `rowHeight` \u548c `estimatedRowHeight` \u5c6c\u6027\u5df2\u6b63\u78ba\u8a2d\u7f6e\u3002<br \/>\n&#8211; \u78ba\u4fdd\u5728 Cell \u4e2d\u7684 Label \u6216\u5176\u4ed6 UI \u5143\u7d20\u7684 `numberOfLines` \u8a2d\u70ba 0\u3002<\/p>\n<p>## 5. \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u900f\u904e\u81ea\u9069\u61c9\u9ad8\u5ea6\u7684\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5728 TableView \u4e2d\u5be6\u73fe\u66f4\u70ba\u8907\u96dc\u7684 UI\uff0c\u4f8b\u5982\u986f\u793a\u5716\u7247\u8207\u6587\u5b57\u7684\u6df7\u5408\u5167\u5bb9\u3002\u9019\u5c07\u5927\u5e45\u63d0\u9ad8\u7528\u6236\u9ad4\u9a57\uff0c\u4f7f\u61c9\u7528\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"\u81ea\u9069\u61c9\u9ad8\u5ea6\u7684\u793a\u7bc4\u5716\u7247\" title=\"\"><br \/>\n<\/a><br \/>\n*\u5716\u7247\u8aaa\u660e\uff1a\u81ea\u9069\u61c9\u9ad8\u5ea6\u7684\u793a\u7bc4\uff0c\u986f\u793a\u5167\u5bb9\u96a8\u884c\u9ad8\u8b8a\u5316\u7684\u6548\u679c\u3002*<\/p>\n<p>## \u5e38\u898b\u554f\u984c\u89e3\u7b54<\/p>\n<p>### Q1: \u5982\u4f55\u8b93 TableView \u7684\u884c\u9ad8\u96a8\u5167\u5bb9\u81ea\u52d5\u8abf\u6574\uff1f<br \/>\nA1: \u4f60\u9700\u8981\u5728 `UITableViewController` \u4e2d\u8a2d\u7f6e `rowHeight` \u70ba `UITableView.automaticDimension`\uff0c\u4e26\u70ba\u81ea\u5b9a\u7fa9 Cell \u7684 UI \u5143\u7d20\u8a2d\u7f6e\u5408\u9069\u7684 Auto Layout \u7d04\u675f\u3002<\/p>\n<p>### Q2: \u5982\u679c\u884c\u9ad8\u4e0d\u81ea\u9069\u61c9\uff0c\u8a72\u5982\u4f55\u6392\u67e5\uff1f<br \/>\nA2: \u78ba\u4fdd\u4f60\u7684 UI \u5143\u7d20\u6709\u6b63\u78ba\u7684 Auto Layout \u7d04\u675f\uff0c\u4e26\u6aa2\u67e5 `numberOfLines` \u5c6c\u6027\u662f\u5426\u8a2d\u70ba 0\u3002<\/p>\n<p>### Q3: \u662f\u5426\u53ef\u4ee5\u5728 TableView \u4e2d\u4f7f\u7528\u5716\u7247\u4e26\u4f7f\u884c\u9ad8\u81ea\u9069\u61c9\uff1f<br \/>\nA3: \u662f\u7684\uff0c\u53ea\u8981\u78ba\u4fdd\u5716\u7247\u7684\u7d04\u675f\u6b63\u78ba\u8a2d\u5b9a\uff0c\u884c\u9ad8\u5c31\u53ef\u4ee5\u81ea\u52d5\u9069\u61c9\u5716\u7247\u7684\u5927\u5c0f\u548c\u5167\u5bb9\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u66f4\u65b0\u4e0d\u50c5\u5f37\u5316\u4e86\u6587\u7ae0\u7684\u5be6\u7528\u6027\uff0c\u9084\u63d0\u9ad8\u4e86 SEO \u6548\u679c\uff0c\u5e0c\u671b\u9019\u80fd\u5e6b\u52a9\u5230\u4f60\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728TableViewController\u4e2d\u5beb\u4e0arowHeight\u7684\u7a0b\u5f0f\u78bc<br \/>\n\u6700\u5f8creturn UITableView.automaticDimension<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-2Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/185\/revisions\/605"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}