{"id":7094,"date":"2025-06-02T14:54:49","date_gmt":"2025-06-05T07:19:48","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7094"},"modified":"2025-06-05T14:54:49","modified_gmt":"2025-06-05T07:19:48","slug":"https-badgameshow-com-steven-222","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-222\/","title":{"rendered":"2025 \u6700\u65b0 Swift UITableView \u591a\u500b Section \u5206\u7d44\u986f\u793a\u6559\u5b78 \ud83d\udd22"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITableView, \u591a\u500bsection, \u5206\u7d44\u986f\u793a, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<h1>2025 \u6700\u65b0 Swift UITableView \u591a\u500b Section \u5206\u7d44\u986f\u793a\u6559\u5b78 \ud83d\udd22<\/h1>\n<p>\u5728\u958b\u767c iOS App \u6642\uff0c\u7d93\u5e38\u9700\u8981\u5c07\u8cc7\u6599\u4ee5\u591a\u500b Section \u5206\u7d44\u986f\u793a\uff0c\u800c UITableView \u6b63\u662f\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u7684\u6700\u4f73\u65b9\u6848\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe UITableView \u7684\u591a\u500b Section \u5206\u7d44\u986f\u793a\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u6b65\u9a5f\u548c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u524d\u7f6e\u6e96\u5099<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b\u65b0\u7684 Xcode \u5c08\u6848\uff0c\u4e26\u5728 Storyboard \u4e2d\u52a0\u5165\u4e00\u500b UITableView\u3002\u63a5\u8457\uff0c\u5c07\u5b83\u7684 delegate \u548c dataSource \u90fd\u9023\u63a5\u5230 ViewController\uff0c\u5982\u4e0b\u5716\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/XKzfXvX.png\" alt=\"UITableView\" title=\"UITableView \u8a2d\u5b9a\u793a\u610f\u5716\" \/><\/p>\n<h2>\u5efa\u7acb\u8cc7\u6599\u6a21\u578b<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b\u8cc7\u6599\u6a21\u578b\u4f86\u5b58\u653e\u6211\u5011\u7684\u8cc7\u6599\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u8cc7\u6599\u6a21\u578b\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct Section {\n    var name: String\n    var items: [String]\n}\n<\/code><\/pre>\n<h2>\u5efa\u7acb\u8cc7\u6599\u4f86\u6e90<\/h2>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b\u8cc7\u6599\u4f86\u6e90\u4f86\u5b58\u653e\u5be6\u969b\u986f\u793a\u7684\u8cc7\u6599\uff0c\u9019\u88e1\u662f\u4e00\u500b\u7c21\u55ae\u7684\u8cc7\u6599\u4f86\u6e90\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let sections = [\n    Section(name: \"Fruits\", items: [\"Apple\", \"Orange\", \"Banana\"]),\n    Section(name: \"Vegetables\", items: [\"Carrot\", \"Potato\", \"Tomato\"])\n]\n<\/code><\/pre>\n<h2>\u5be6\u73fe UITableViewDataSource \u5354\u8b70<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5be6\u73fe UITableViewDataSource \u5354\u8b70\u4e2d\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5c07\u8cc7\u6599\u986f\u793a\u5728 UITableView \u4e0a\u3002\u4ee5\u4e0b\u662f\u5177\u9ad4\u7684\u5be6\u73fe\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func numberOfSections(in tableView: UITableView) -> Int {\n    return sections.count\n}\n\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n    return sections[section].items.count\n}\n\nfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n    let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n    let item = sections[indexPath.section].items[indexPath.row]\n    cell.textLabel?.text = item\n    return cell\n}\n\nfunc tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {\n    return sections[section].name\n}\n<\/code><\/pre>\n<h2>\u5be6\u73fe UITableViewDelegate \u5354\u8b70<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5be6\u73fe UITableViewDelegate \u5354\u8b70\u4e2d\u7684\u65b9\u6cd5\uff0c\u9019\u6a23\u624d\u80fd\u5c0d UITableView \u9032\u884c\u4e00\u4e9b\u57fa\u672c\u8a2d\u5b9a\uff0c\u4ee5\u4e0b\u662f\u5177\u9ad4\u7684\u65b9\u6cd5\u5be6\u73fe\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {\n    return 44\n}\n\nfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {\n    tableView.deselectRow(at: indexPath, animated: true)\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982 Cell \u7121\u6cd5\u6b63\u78ba\u986f\u793a\u6216\u8cc7\u6599\u4e0d\u6b63\u78ba\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6392\u9664\u932f\u8aa4\u7684\u5efa\u8b70\uff1a<\/p>\n<p>1. \u78ba\u8a8d Storyboard \u4e2d\u7684 UITableViewCell Identifier \u662f\u5426\u6b63\u78ba\u8a2d\u5b9a\u70ba &#8220;Cell&#8221;\u3002<br \/>\n2. \u78ba\u4fdd delegate \u548c dataSource \u90fd\u5df2\u6b63\u78ba\u9023\u63a5\u5230 ViewController\u3002<br \/>\n3. \u4f7f\u7528 print() \u8a9e\u53e5\u4f86\u6aa2\u67e5\u8cc7\u6599\u4f86\u6e90\u662f\u5426\u6b63\u78ba\u50b3\u905e\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684 Section \u5206\u7d44\u986f\u793a\u5916\uff0c\u4f60\u9084\u53ef\u4ee5\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; \u52a0\u5165\u81ea\u8a02\u7684 UITableViewCell \u4f86\u63d0\u5347 UI\u3002<br \/>\n&#8211; \u5be6\u4f5c\u641c\u5c0b\u529f\u80fd\uff0c\u8b93\u7528\u6236\u80fd\u5920\u5feb\u901f\u627e\u5230\u6240\u9700\u9805\u76ee\u3002<br \/>\n&#8211; \u6574\u5408\u7db2\u8def\u8cc7\u6599\u4f86\u6e90\uff0c\u52d5\u614b\u52a0\u8f09\u8cc7\u6599\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u5df2\u7d93\u6210\u529f\u5be6\u73fe\u4e86 Swift \u4e2d\u7684 UITableView \u591a\u500b Section \u5206\u7d44\u986f\u793a\u529f\u80fd\uff0c\u8b93\u8cc7\u6599\u4ee5\u66f4\u6e05\u6670\u7684\u65b9\u5f0f\u5448\u73fe\uff0c\u63d0\u5347\u4e86\u4f7f\u7528\u8005\u7684\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UITableView \u5982\u4f55\u8655\u7406\u5927\u91cf\u8cc7\u6599\uff1f<\/h3>\n<p>A1: \u4f7f\u7528 UITableView \u7684\u5206\u9801\u52a0\u8f09\u6216\u7121\u9650\u6efe\u52d5\u529f\u80fd\u4f86\u52d5\u614b\u52a0\u8f09\u8cc7\u6599\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h3>Q2: UITableView \u53ef\u4ee5\u986f\u793a\u5716\u7247\u55ce\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\uff0c\u900f\u904e\u81ea\u8a02 UITableViewCell \u4f86\u986f\u793a\u5716\u7247\u53ca\u6587\u5b57\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u81ea\u8a02 UITableViewCell \u7684\u6a23\u5f0f\uff1f<\/h3>\n<p>A3: \u5728 Storyboard \u4e2d\u9078\u64c7 UITableViewCell\uff0c\u7136\u5f8c\u4fee\u6539\u5176\u5c6c\u6027\u6216\u4f7f\u7528\u81ea\u8a02\u7684 XIB \u6587\u4ef6\u4f86\u8a2d\u8a08\u3002<\/p>\n<h2>\u63a8\u85a6\u95b1\u8b80\u6587\u7ae0<\/h2>\n<p><a href=\"https:\/\/www.appcoda.com.tw\/uitableview-multiple-sections\/\" target=\"_blank\" rel=\"noopener\">Swift UITableView \u591a\u500b Section \u5206\u7d44\u986f\u793a<\/a><br \/>\n<a href=\"https:\/\/www.appcoda.com.tw\/uitableview-programmatically\/\" target=\"_blank\" rel=\"noopener\">Swift UITableView \u7a0b\u5f0f\u78bc\u5efa\u7acb<\/a><br \/>\n<a href=\"https:\/\/www.appcoda.com.tw\/uitableview-customize\/\" target=\"_blank\" rel=\"noopener\">Swift UITableView \u81ea\u8a02 Cell<\/a><br \/>\n<a href=\"https:\/\/www.appcoda.com.tw\/uitableview-dynamic-prototype-cells\/\" target=\"_blank\" rel=\"noopener\">Swift UITableView \u52d5\u614b\u539f\u578b Cell<\/a><br \/>\n<a href=\"https:\/\/www.appcoda.com.tw\/uitableview-static-cells\/\" target=\"_blank\" rel=\"noopener\">Swift UITableView \u975c\u614b Cell<\/a><\/p>\n<h2>\u5ef6\u4f38\u95b1\u8b80\u672c\u7ad9\u6587\u7ae0<\/h2>\n<p><a href=\"https:\/\/coin028.com\/category\/swift\/\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a Swift \u76f8\u95dc\u6587\u7ae0<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView-\u591a\u500bsection-\u5206\u7d44\u986f\u793a-\ud83d\udd22_1675402366.970559.webp\" alt=\"Swift UITableView \u591a\u500b Section \u5206\u7d44\u986f\u793a \ud83d\udd22\" title=\"Swift UITableView, \u591a\u500b Section, \u5206\u7d44\u986f\u793a\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftUITableView\u5be6\u73fe\u591a\u500bSection\u5206\u7d44\u986f\u793a\uff0c\u8a73\u7d30\u4ecb\u7d39SwiftUITableView\u5982\u4f55\u8a2d\u7f6e\u591a\u500bSection\uff0c\u4ee5\u53ca\u5982\u4f55\u5c0d\u6bcf\u500bSection\u9032\u884c\u8a2d\u7f6e\uff0c\u8b93\u4f60\u53ef\u4ee5\u5feb\u901f\u7684\u5be6\u73fe\u591a\u500bSection\u5206\u7d44\u986f\u793a\u3002<\/p>\n","protected":false},"author":1,"featured_media":7093,"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-7094","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-UITableView-\u591a\u500bsection-\u5206\u7d44\u986f\u793a-\ud83d\udd22_1675402366.970559.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Qq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7094","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=7094"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7094\/revisions"}],"predecessor-version":[{"id":13290,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7094\/revisions\/13290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7093"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}