{"id":6705,"date":"2025-06-03T12:38:57","date_gmt":"2025-06-05T04:56:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6705"},"modified":"2025-06-05T12:38:57","modified_gmt":"2025-06-05T04:56:56","slug":"https-badgameshow-com-steven-30","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-30\/","title":{"rendered":"Swift UITableView \u4f7f\u7528\u6307\u5357\u30102025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UITableView, \u64cd\u4f5c, \u6559\u5b78, iOS \u958b\u767c, Swift \u6559\u5b78, UITableView \u6559\u5b78\"><\/p>\n<h1>Swift UITableView \u4f7f\u7528\u6307\u5357\u30102025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3011<\/h1>\n<p>Swift UITableView \u662f iOS \u958b\u767c\u4e2d\u6700\u5e38\u7528\u7684\u5143\u4ef6\u4e4b\u4e00\uff0c\u5b83\u4f7f\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u5efa\u7acb\u8868\u683c\uff0c\u6539\u5584\u4f7f\u7528\u8005\u7684\u8cc7\u6599\u95b1\u8b80\u9ad4\u9a57\u3002\u672c\u6587\u5c07\u6df1\u5165\u4ecb\u7d39 UITableView \u7684\u57fa\u672c\u64cd\u4f5c\u3001\u6700\u4f73\u5be6\u8e10\uff0c\u4ee5\u53ca\u5e38\u898b\u554f\u984c\u7684\u89e3\u6c7a\u65b9\u6cd5\uff0c\u5e6b\u52a9\u60a8\u8fc5\u901f\u4e0a\u624b\u4e26\u958b\u767c\u7b26\u5408\u73fe\u4ee3\u9700\u6c42\u7684\u8868\u683c\u3002<\/p>\n<h2>\u5efa\u7acb UITableView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 Storyboard \u4e2d\u5efa\u7acb\u4e00\u500b UITableView\u3002\u5c07 UITableView \u62d6\u62fd\u5230 View Controller \u4e2d\uff0c\u4e26\u5c07\u5176 delegate \u8207 dataSource \u9023\u63a5\u5230 View Controller \u4e0a\uff0c\u5982\u4e0b\u5716\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/Xh6yfVU.png\" alt=\"UITableView\" title=\"\u5728 Storyboard \u4e2d\u5efa\u7acb UITableView\" \/><\/p>\n<h3>\u5be6\u4f5c UITableViewDelegate \u8207 UITableViewDataSource<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u8981\u5728 View Controller \u4e2d\u5be6\u4f5c UITableViewDelegate \u8207 UITableViewDataSource \u5169\u500b protocol\u3002\u9019\u5305\u62ec\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n    return 10 \/\/ \u8fd4\u56de\u986f\u793a\u7684\u884c\u6578\n}\n\nfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n    let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n    cell.textLabel?.text = \"Hello World\" \/\/ \u8a2d\u5b9a\u6bcf\u4e00\u5217\u7684\u5167\u5bb9\n    return cell\n}\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a UITableViewCell<\/h2>\n<p>\u5728 Storyboard \u4e2d\uff0c\u5c07 UITableViewCell \u62d6\u62fd\u5230 UITableView \u4e2d\uff0c\u4e26\u5c07\u5176 identifier \u8a2d\u5b9a\u70ba &#8220;Cell&#8221;\u3002\u9019\u6a23\u5c31\u53ef\u4ee5\u5728 cellForRowAt \u65b9\u6cd5\u4e2d\u8a2d\u5b9a\u6bcf\u4e00\u5217\u7684\u5167\u5bb9\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/3q3yh3K.png\" alt=\"UITableViewCell\" title=\"\u8a2d\u5b9a UITableViewCell \u7684 identifier\" \/><\/p>\n<h3>\u66f4\u65b0 cellForRowAt \u65b9\u6cd5<\/h3>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n    let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n    cell.textLabel?.text = \"Hello World\" \/\/ \u53ef\u4ee5\u6839\u64da indexPath.row \u8a2d\u5b9a\u4e0d\u540c\u7684\u5167\u5bb9\n    return cell\n}\n<\/code><\/pre>\n<h2>\u52a0\u5165 UITableViewCell \u7684\u52d5\u4f5c<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u70ba UITableViewCell \u52a0\u5165\u9ede\u64ca\u4e8b\u4ef6\uff0c\u8b93\u4f7f\u7528\u8005\u57f7\u884c\u7279\u5b9a\u7684\u52d5\u4f5c\u3002\u5728 UITableViewDelegate \u4e2d\u5be6\u4f5c didSelectRowAt \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {\n    print(\"\u60a8\u9ede\u64ca\u4e86\u7b2c \\(indexPath.row) \u884c\u7684\u55ae\u5143\u683c\u3002\")\n    \/\/ \u5728\u9019\u88e1\u53ef\u4ee5\u6dfb\u52a0\u5176\u4ed6\u52d5\u4f5c\uff0c\u4f8b\u5982\u5c0e\u822a\u5230\u65b0\u9801\u9762\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 UITableView \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff0c\u5982 cell \u7121\u6cd5\u986f\u793a\u6216 indexPath \u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u78ba\u8a8d identifier \u6b63\u78ba\uff1a<\/strong>\u78ba\u4fdd\u60a8\u5728 Storyboard \u4e2d\u8a2d\u5b9a\u7684 identifier \u8207\u4ee3\u78bc\u4e2d\u4f7f\u7528\u7684\u4e00\u81f4\u3002<\/li>\n<li><strong>\u6aa2\u67e5 delegate \u8207 dataSource \u8a2d\u5b9a\uff1a<\/strong>\u78ba\u4fdd UITableView \u7684 delegate \u548c dataSource \u5df2\u6b63\u78ba\u9023\u63a5\u5230 View Controller\u3002<\/li>\n<li><strong>\u91cd\u65b0\u8f09\u5165\u8cc7\u6599\uff1a<\/strong>\u5982\u679c\u8cc7\u6599\u672a\u986f\u793a\uff0c\u8acb\u78ba\u4fdd\u5728\u8cc7\u6599\u66f4\u65b0\u5f8c\u8abf\u7528 <code>tableView.reloadData()<\/code>\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5217\u8868\u986f\u793a\uff0cUITableView \u9084\u53ef\u4ee5\u7528\u65bc\u5275\u5efa\u8907\u96dc\u7684\u754c\u9762\uff0c\u4f8b\u5982\u5e36\u6709\u5716\u7247\u7684\u5217\u8868\u3001\u53ef\u7de8\u8f2f\u7684\u5217\u8868\u7b49\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u81ea\u5b9a\u7fa9 UITableViewCell \u4f86\u9054\u5230\u9019\u4e9b\u6548\u679c\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u4ee5\u4e0a\u5c31\u662f UITableView \u7684\u57fa\u672c\u64cd\u4f5c\u8207\u6559\u5b78\u3002\u900f\u904e Swift \u8a9e\u8a00\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u5efa\u7acb\u51fa\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u8868\u683c\uff0c\u4e26\u70ba\u4f7f\u7528\u8005\u63d0\u4f9b\u826f\u597d\u7684\u64cd\u4f5c\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UITableView \u8207 UICollectionView \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>UITableView \u7528\u65bc\u5782\u76f4\u5217\u8868\u986f\u793a\u8cc7\u6599\uff0c\u800c UICollectionView \u5247\u9069\u5408\u7528\u65bc\u66f4\u9748\u6d3b\u7684\u683c\u72c0\u5e03\u5c40\uff0c\u652f\u6301\u591a\u7a2e\u6392\u5217\u65b9\u5f0f\u3002<\/p>\n<h3>2. \u5982\u4f55\u8b93 UITableView \u652f\u6301\u7de8\u8f2f\u6a21\u5f0f\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>setEditing(_:animated:)<\/code> \u65b9\u6cd5\u4f86\u555f\u7528\u7de8\u8f2f\u6a21\u5f0f\uff0c\u4e26\u5728 <code>tableView(_:canEditRowAt:)<\/code> \u65b9\u6cd5\u4e2d\u6307\u660e\u53ef\u7de8\u8f2f\u7684\u884c\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728 UITableView \u4e2d\u4f7f\u7528\u81ea\u5b9a\u7fa9 cell\uff1f<\/h3>\n<p>\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684 UITableViewCell \u985e\uff0c\u4e26\u5728 Storyboard \u4e2d\u8a2d\u7f6e\u5176 identifier\uff0c\u7136\u5f8c\u5728 <code>cellForRowAt<\/code> \u65b9\u6cd5\u4e2d\u4f7f\u7528 <code>dequeueReusableCell<\/code> \u65b9\u6cd5\u4f86\u8fd4\u56de\u81ea\u5b9a\u7fa9 cell\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView\u4f7f\u7528\u270d\ufe0f-\u57fa\u672c\u8868\u683c\u7684\u64cd\u4f5c\u8207\u6559\u5b78_1675394148.779246.webp\" alt=\"Swift UITableView\u4f7f\u7528\u270d\ufe0f- \u57fa\u672c\u8868\u683c\u7684\u64cd\u4f5c\u8207\u6559\u5b78\" title=\"Swift UITableView \u4f7f\u7528\u6307\u5357\uff0c\u8868\u683c\u64cd\u4f5c\uff0cSwift \u6559\u5b78\uff0cUITableView \u6559\u5b78\uff0cSwift UITableView \u6559\u5b78\" width=\"1200\" height=\"628\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528SwiftUITableView\uff0c\u4e86\u89e3\u57fa\u672c\u8868\u683c\u7684\u64cd\u4f5c\u8207\u6559\u5b78\uff0c\u8b93\u4f60\u80fd\u5920\u66f4\u8f15\u9b06\u5730\u4f7f\u7528SwiftUITableView\uff0c\u66f4\u5feb\u5730\u5b8c\u6210\u4f60\u7684\u5de5\u4f5c\u3002<\/p>\n","protected":false},"author":1,"featured_media":6703,"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-6705","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\u4f7f\u7528\u270d\ufe0f-\u57fa\u672c\u8868\u683c\u7684\u64cd\u4f5c\u8207\u6559\u5b78_1675394148.779246.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1K9","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6705","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=6705"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6705\/revisions"}],"predecessor-version":[{"id":6745,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6705\/revisions\/6745"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6703"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}