{"id":7288,"date":"2025-06-03T12:38:52","date_gmt":"2025-06-05T05:07:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7288"},"modified":"2025-06-05T12:38:52","modified_gmt":"2025-06-05T05:07:51","slug":"https-badgameshow-com-steven-315","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-315\/","title":{"rendered":"Swift UITableView \u5b8c\u5168\u6307\u5357 &#8211; 2025 \u6700\u65b0\u7684 UITableViewDataSource \u5be6\u4f5c\u6559\u7a0b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UITableView, UITableViewDataSource, iOS\u958b\u767c, Swift\u6559\u5b78, UITableView\u5be6\u4f5c\"><\/p>\n<h1>Swift UITableView \u5b8c\u5168\u6307\u5357 &#8211; 2025 \u6700\u65b0\u7684 UITableViewDataSource \u5be6\u4f5c\u6559\u7a0b<\/h1>\n<p>\u5728iOS\u958b\u767c\u4e2d\uff0c`UITableView`\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5143\u4ef6\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u5feb\u901f\u5efa\u7acb\u8868\u683c\u5f0f\u4ecb\u9762\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u8f15\u9b06\u67e5\u770b\u8cc7\u6599\u3002\u70ba\u4e86\u6709\u6548\u5730\u4f7f\u7528`UITableView`\uff0c\u958b\u767c\u8005\u5fc5\u9808\u4e86\u89e3`UITableViewDataSource`\u5354\u5b9a\uff0c\u9019\u662f\u63a7\u5236`UITableView`\u8cc7\u6599\u4f86\u6e90\u548c`UITableViewCell`\u986f\u793a\u65b9\u5f0f\u7684\u95dc\u9375\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f UITableViewDataSource\uff1f<\/h2>\n<p>`UITableViewDataSource`\u662f\u4e00\u500b\u5354\u5b9a\uff0c\u5b9a\u7fa9\u4e86`UITableView`\u9700\u8981\u5be6\u4f5c\u7684\u65b9\u6cd5\uff0c\u4ee5\u6b63\u78ba\u986f\u793a\u8cc7\u6599\u3002\u5be6\u4f5c\u6b64\u5354\u5b9a\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63a7\u5236\u8868\u683c\u7684\u884c\u6578\u548c\u6bcf\u500b\u884c\u7684\u5167\u5bb9\u3002<\/p>\n<h2>\u958b\u59cb\u4f7f\u7528 UITableView<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5efa\u7acb\u4e00\u500b`UITableView`\u7684\u5be6\u4f8b\u4e26\u8a2d\u5b9a\u5176\u8cc7\u6599\u4f86\u6e90\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u9032\u884c\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let tableView = UITableView()\ntableView.dataSource = self\n<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u60a8\u9700\u8981\u9075\u5faa`UITableViewDataSource`\u5354\u5b9a\u4e26\u5be6\u4f5c\u5fc5\u8981\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension YourViewController: UITableViewDataSource {\n    func numberOfSections(in tableView: UITableView) -> Int {\n        \/\/ \u56de\u50b3\u8981\u986f\u793a\u7684section\u6578\u91cf\n        return 1\n    }\n\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n        \/\/ \u56de\u50b3\u6bcf\u500bsection\u8981\u986f\u793a\u7684row\u6578\u91cf\n        return 10\n    }\n\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n        \/\/ \u56de\u50b3\u6bcf\u500bcell\u8981\u986f\u793a\u7684\u5167\u5bb9\n        let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n        cell.textLabel?.text = \"Cell \\(indexPath.row)\"\n        return cell\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff1a<br \/>\n&#8211; \u6211\u5011\u5be6\u4f5c\u4e86`numberOfSections(in:)`\u65b9\u6cd5\uff0c\u56de\u50b3\u8981\u986f\u793a\u7684section\u6578\u91cf\u3002<br \/>\n&#8211; \u63a5\u8457\u662f`tableView(_:numberOfRowsInSection:)`\u65b9\u6cd5\uff0c\u9019\u88e1\u56de\u50b3\u6bcf\u500bsection\u4e2d\u7684row\u6578\u91cf\u3002<br \/>\n&#8211; \u6700\u5f8c\uff0c`tableView(_:cellForRowAt:)`\u65b9\u6cd5\u7528\u4f86\u914d\u7f6e\u6bcf\u500bcell\u7684\u5167\u5bb9\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u60a8\u5728\u5be6\u4f5c\u6642\u9047\u5230\u554f\u984c\uff0c\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd\u60a8\u7684`UITableView`\u7684dataSource\u6b63\u78ba\u8a2d\u5b9a\u70ba\u7576\u524d\u7684\u8996\u5716\u63a7\u5236\u5668\u3002<br \/>\n&#8211; \u78ba\u8a8dcell\u7684\u91cd\u7528\u8b58\u5225\u7b26\uff08reuse identifier\uff09\u8207\u60a8\u5728`dequeueReusableCell`\u4e2d\u4f7f\u7528\u7684\u4e00\u81f4\u3002<br \/>\n&#8211; \u6aa2\u67e5`UITableView`\u662f\u5426\u6709\u9023\u63a5\u81f3Interface Builder\uff08\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fStoryboard\uff09\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`UITableView`\u4e0d\u50c5\u53ef\u4ee5\u986f\u793a\u975c\u614b\u8cc7\u6599\uff0c\u9084\u53ef\u4ee5\u8207\u52d5\u614b\u8cc7\u6599\u4f86\u6e90\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u5f9e\u7db2\u8defAPI\u7372\u53d6\u8cc7\u6599\u3002\u60a8\u53ef\u4ee5\u8003\u616e\u4f7f\u7528`URLSession`\u4f86\u7372\u53d6JSON\u6578\u64da\uff0c\u7136\u5f8c\u5c07\u5176\u89e3\u6790\u4e26\u986f\u793a\u5728`UITableView`\u4e2d\u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>`UITableView`\u548c`UITableViewDataSource`\u662fiOS\u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\u3002\u900f\u904e\u672c\u7bc7\u6559\u7a0b\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u638c\u63e1\u5982\u4f55\u4f7f\u7528\u9019\u4e9b\u5de5\u5177\u4f86\u5efa\u7acb\u9ad8\u6548\u7684\u8868\u683c\u754c\u9762\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView-\u61c9\u7528\u57fa\u672c\u7684UITableViewDataSource-\ud83d\udc4c_1675410110.324899.webp\" alt=\"Swift UITableView - \u61c9\u7528\u57fa\u672c\u7684UITableViewDataSource \ud83d\udc4c\" title=\"Swift UITableViewDataSource\u3001UITableViewDataSource\u3001iOS\u61c9\u7528\u7a0b\u5e8f\u3001Swift\u7a0b\u5f0f\u8a9e\u6cd5\u3001\u53ef\u64f4\u5c55\u6027\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UITableView \u548c UICollectionView \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>UITableView \u662f\u7528\u65bc\u986f\u793a\u4e00\u7dad\u5217\u8868\u7684\uff0c\u800c UICollectionView \u5247\u7528\u65bc\u986f\u793a\u4e8c\u7dad\u7db2\u683c\u4f48\u5c40\uff0c\u63d0\u4f9b\u66f4\u591a\u7684\u4f48\u5c40\u9078\u64c7\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728 UITableView \u4e2d\u6dfb\u52a0\u522a\u9664\u529f\u80fd\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 UITableViewDataSource \u4e2d\u5be6\u4f5c <code>tableView(_:commit:forRowAt:)<\/code> \u65b9\u6cd5\uff0c\u8655\u7406\u522a\u9664\u908f\u8f2f\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u6709\u591a\u500b Section \u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728 <code>numberOfSections(in:)<\/code> \u65b9\u6cd5\u4e2d\u8fd4\u56de\u591a\u500b section\uff0c\u4e26\u5728 <code>tableView(_:numberOfRowsInSection:)<\/code> \u65b9\u6cd5\u4e2d\u70ba\u6bcf\u500b section \u63d0\u4f9b\u4e0d\u540c\u7684 row \u6578\u91cf\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftUITableViewDataSource\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u61c9\u7528\u57fa\u672c\u7684UITableViewDataSource\uff0c\u4ee5\u69cb\u5efa\u66f4\u5177\u53ef\u64f4\u5c55\u6027\u7684iOS\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":7285,"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-7288","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-\u61c9\u7528\u57fa\u672c\u7684UITableViewDataSource-\ud83d\udc4c_1675410110.324899.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ty","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7288","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=7288"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7285"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}