{"id":7459,"date":"2025-06-01T15:03:01","date_gmt":"2025-06-05T07:11:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7459"},"modified":"2025-06-05T15:03:01","modified_gmt":"2025-06-05T07:11:00","slug":"https-badgameshow-com-steven-400","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-400\/","title":{"rendered":"Swift 2025 \u6700\u65b0\u6559\u5b78\uff1a\u8a2d\u5b9a TableView Cell \u9ad8\u5ea6\u81ea\u9069\u61c9\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"Swift, TableView, Cell, \u9ad8\u5ea6\u81ea\u9069\u61c9, \u81ea\u52d5\u8abf\u6574, iOS \u958b\u767c\"><\/p>\n<h2>Swift \u8a2d\u5b9a TableView Cell \u9ad8\u5ea6\u81ea\u9069\u61c9\u7684\u5fc5\u8981\u6027<\/h2>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u4f7f\u7528 <strong>UITableView<\/strong> \u6642\uff0c\u8a2d\u5b9a Cell \u9ad8\u5ea6\u81ea\u9069\u61c9\u662f\u4e00\u500b\u5e38\u898b\u9700\u6c42\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u80fd\u6709\u6548\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\uff0c\u8b93\u754c\u9762\u66f4\u52a0\u7f8e\u89c0\u4e14\u7b26\u5408\u4e0d\u540c\u5167\u5bb9\u7684\u9577\u5ea6\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 2025 \u5e74\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u4f86\u8a2d\u5b9a TableView Cell \u7684\u9ad8\u5ea6\u81ea\u9069\u61c9\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u5be6\u73fe UITableViewDelegate \u7684 heightForRowAt \u65b9\u6cd5<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>UITableViewDelegate<\/strong> \u4e2d\u5be6\u73fe <strong>heightForRowAt<\/strong> \u65b9\u6cd5\uff0c\u4ee5\u8a08\u7b97\u6bcf\u500b Cell \u7684\u9ad8\u5ea6\u3002\u9019\u88e1\u6211\u5011\u4f7f\u7528\u4e86 <code>UITableView.automaticDimension<\/code> \u4f86\u8b93 Cell \u9ad8\u5ea6\u81ea\u52d5\u8abf\u6574\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {\n    return UITableView.automaticDimension\n}\n<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u5be6\u73fe UITableViewDataSource \u7684 estimatedHeightForRowAt \u65b9\u6cd5<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>UITableViewDataSource<\/strong> \u4e2d\u5be6\u73fe <strong>estimatedHeightForRowAt<\/strong> \u65b9\u6cd5\uff0c\u9019\u500b\u65b9\u6cd5\u7528\u4f86\u63d0\u4f9b Cell \u7684\u9810\u4f30\u9ad8\u5ea6\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {\n    return UITableView.automaticDimension\n}\n<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u8a2d\u5b9a UITableView \u7684 rowHeight \u5c6c\u6027<\/h2>\n<p>\u6700\u5f8c\uff0c\u4f60\u9700\u8981\u5728 <strong>UITableView<\/strong> \u7684\u8a2d\u5b9a\u4e2d\u6307\u5b9a <strong>rowHeight<\/strong> \u5c6c\u6027\u70ba <code>UITableView.automaticDimension<\/code>\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">tableView.rowHeight = UITableView.automaticDimension\n<\/code><\/pre>\n<h2>\u5b8c\u6574\u7bc4\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u4e00\u500b\u7c21\u55ae\u7684 TableView \u4e2d\u5be6\u73fe\u81ea\u9069\u61c9 Cell \u9ad8\u5ea6\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import UIKit\n\nclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {\n    @IBOutlet weak var tableView: UITableView!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        tableView.delegate = self\n        tableView.dataSource = self\n        tableView.rowHeight = UITableView.automaticDimension\n        tableView.estimatedRowHeight = 44.0 \/\/ \u9810\u8a2d\u9ad8\u5ea6\n    }\n\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n        return 20 \/\/ \u7bc4\u4f8b\u8cc7\u6599\u6578\u91cf\n    }\n\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n        let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n        cell.textLabel?.text = \"\u9019\u662f\u7b2c \\(indexPath.row + 1) \u884c\u7684\u5167\u5bb9\uff0c\u53ef\u80fd\u6703\u6709\u4e0d\u540c\u7684\u9577\u5ea6\u3002\"\n        return cell\n    }\n\n    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {\n        return UITableView.automaticDimension\n    }\n\n    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {\n        return UITableView.automaticDimension\n    }\n}\n<\/code><\/pre>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u82e5 Cell \u9ad8\u5ea6\u672a\u6309\u9810\u671f\u81ea\u52d5\u8abf\u6574\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd Cell \u7684\u5167\u5bb9\u7d04\u675f\u689d\u4ef6\uff08Constraints\uff09\u6b63\u78ba\u8a2d\u7f6e\uff0c\u4ee5\u4fbf Auto Layout \u80fd\u5920\u6b63\u78ba\u8a08\u7b97\u9ad8\u5ea6\u3002<\/li>\n<li>\u78ba\u8a8d\u5df2\u5728 <strong>UITableView<\/strong> \u7684\u4ee3\u7406\u548c\u6578\u64da\u6e90\u65b9\u6cd5\u4e2d\u6b63\u78ba\u5be6\u73fe\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u3002<\/li>\n<li>\u6aa2\u67e5\u662f\u5426\u70ba Cell \u8a2d\u5b9a\u4e86\u56fa\u5b9a\u7684\u9ad8\u5ea6\uff0c\u9019\u6703\u5f71\u97ff\u81ea\u52d5\u8abf\u6574\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684 Cell \u9ad8\u5ea6\u81ea\u9069\u61c9\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u9032\u968e\u61c9\u7528\uff1a<\/p>\n<ul>\n<li>\u7d50\u5408 <strong>UICollectionView<\/strong> \u5be6\u73fe\u66f4\u52a0\u9748\u6d3b\u7684\u5e03\u5c40\u3002<\/li>\n<li>\u4f7f\u7528 <strong>SwiftUI<\/strong> \u7684 <code>List<\/code> \u7d44\u4ef6\u4f86\u7c21\u5316\u958b\u767c\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8a2d\u5b9a-TableView-Cell-\u9ad8\u5ea6\u81ea\u9069\u61c9_1675476251.7984052.webp\" alt=\"Swift \u8a2d\u5b9a TableView Cell \u9ad8\u5ea6\u81ea\u9069\u61c9\" title=\"Swift \u8a2d\u5b9a TableView Cell\u3001TableView Cell \u9ad8\u5ea6\u81ea\u9069\u61c9\u3001Swift TableView Cell\u3001TableView Cell \u8a2d\u5b9a\u3001TableView Cell \u9ad8\u5ea6\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u70ba\u4ec0\u9ebc\u6211\u7684 TableView Cell \u9ad8\u5ea6\u4e0d\u6703\u81ea\u52d5\u8abf\u6574\uff1f<\/h3>\n<p>A1: \u8acb\u6aa2\u67e5 Cell \u7684 Auto Layout \u7d04\u675f\u662f\u5426\u8a2d\u7f6e\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u6c92\u6709\u5728 TableView \u4e2d\u5f37\u5236\u8a2d\u7f6e Cell \u7684\u9ad8\u5ea6\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u4f7f\u7528 SwiftUI \u5be6\u73fe\u81ea\u9069\u61c9\u9ad8\u5ea6\u7684\u5217\u8868\uff1f<\/h3>\n<p>A2: \u5728 SwiftUI \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>List<\/code> \u7d44\u4ef6\uff0c\u4e26\u8b93\u5176\u5167\u5bb9\u81ea\u52d5\u8abf\u6574\u9ad8\u5ea6\uff0c\u7121\u9700\u984d\u5916\u8a2d\u5b9a\u3002<\/p>\n<h3>Q3: \u5728 UITableView \u4e2d\u4f7f\u7528\u81ea\u9069\u61c9 Cell \u9ad8\u5ea6\u6703\u5f71\u97ff\u6027\u80fd\u55ce\uff1f<\/h3>\n<p>A3: \u4e00\u822c\u4f86\u8aaa\uff0c\u4f7f\u7528\u81ea\u9069\u61c9\u9ad8\u5ea6\u4e0d\u6703\u986f\u8457\u5f71\u97ff\u6027\u80fd\uff0c\u4f46\u5728\u5927\u91cf\u8cc7\u6599\u7684\u60c5\u6cc1\u4e0b\uff0c\u5efa\u8b70\u9032\u884c\u6027\u80fd\u6e2c\u8a66\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u8a2d\u5b9aTableViewCell\u9ad8\u5ea6\u81ea\u9069\u61c9\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c0dTableViewCell\u9ad8\u5ea6\u9032\u884c\u8a2d\u5b9a\uff0c\u4ee5\u9054\u5230\u66f4\u597d\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":7458,"comment_status":"open","ping_status":"closed","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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8a2d\u5b9a-TableView-Cell-\u9ad8\u5ea6\u81ea\u9069\u61c9_1675476251.7984052.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Wj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7459","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=7459"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7459\/revisions"}],"predecessor-version":[{"id":13610,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7459\/revisions\/13610"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7458"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}