{"id":7050,"date":"2025-06-03T12:51:18","date_gmt":"2025-06-05T05:37:17","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7050"},"modified":"2025-06-05T12:51:18","modified_gmt":"2025-06-05T05:37:17","slug":"https-badgameshow-com-steven-201","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-201\/","title":{"rendered":"Swift UICollectionView \u5b8c\u6574\u6559\u5b78\uff1a\u81ea\u5b9a\u7fa9\u5340\u584a\u7684\u6700\u4f73\u5be6\u8e10 \ud83c\udfa8\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UICollectionView, \u81ea\u5b9a\u7fa9\u5340\u584a, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<h2>\u4ec0\u9ebc\u662f Swift UICollectionView\uff1f<\/h2>\n<p>\nSwift UICollectionView \u662f\u4e00\u500b\u5f37\u5927\u7684 iOS \u5143\u4ef6\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u9748\u6d3b\u5730\u5275\u5efa\u548c\u986f\u793a\u81ea\u5b9a\u7fa9\u7684\u5340\u584a\u3002\u9019\u500b\u5143\u4ef6\u4e0d\u50c5\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u8cc7\u6599\uff0c\u9084\u80fd\u63d0\u4f9b\u9ad8\u5ea6\u81ea\u5b9a\u7fa9\u7684\u8996\u89ba\u6548\u679c\uff0c\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\u66f4\u52a0\u7f8e\u89c0\u548c\u7b26\u5408\u7528\u6236\u9700\u6c42\u3002\n<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Swift UICollectionView\uff1f<\/h2>\n<p>\n\u4f7f\u7528 Swift UICollectionView \u76f8\u5c0d\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u5f9e\u8a2d\u7f6e\u5230\u986f\u793a\u8cc7\u6599\u7684\u5b8c\u6574\u6b65\u9a5f\u3002\n<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u5728 Xcode \u4e2d\u5275\u5efa UICollectionView<\/h3>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5efa\u7acb UICollectionView\nlet layout = UICollectionViewFlowLayout()\nlet collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)\n\n\/\/ \u8a2d\u5b9a UICollectionView \u7684 delegate \u548c dataSource\ncollectionView.delegate = self\ncollectionView.dataSource = self\n\n\/\/ \u8a3b\u518a UICollectionViewCell\ncollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: \"Cell\")\n\n\/\/ \u5c07 UICollectionView \u52a0\u5165\u5230\u756b\u9762\u4e2d\nview.addSubview(collectionView)\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 2\uff1a\u5be6\u4f5c UICollectionViewDelegate \u548c UICollectionViewDataSource \u5354\u5b9a<\/h3>\n<p>\n\u70ba\u4e86\u8b93 UICollectionView \u6b63\u78ba\u986f\u793a\u8cc7\u6599\uff0c\u4f60\u9700\u8981\u5be6\u4f5c <code>UICollectionViewDelegate<\/code> \u548c <code>UICollectionViewDataSource<\/code> \u5354\u5b9a\uff1a\n<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5be6\u4f5c UICollectionViewDelegate \u548c UICollectionViewDataSource \u5169\u500b\u5354\u5b9a\nextension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {\n    \/\/ \u8a2d\u5b9a UICollectionView \u7684\u7e3d\u5171\u6709\u5e7e\u500b section\n    func numberOfSections(in collectionView: UICollectionView) -> Int {\n        return 1\n    }\n\n    \/\/ \u8a2d\u5b9a\u6bcf\u500b section \u4e2d\u6709\u5e7e\u500b item\n    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n        return 10 \/\/ \u53ef\u6839\u64da\u4f60\u7684\u8cc7\u6599\u52d5\u614b\u8fd4\u56de\n    }\n\n    \/\/ \u8a2d\u5b9a\u6bcf\u500b item \u7684\u5167\u5bb9\n    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"Cell\", for: indexPath)\n        cell.backgroundColor = .red \/\/ \u9019\u88e1\u53ef\u4ee5\u81ea\u5b9a\u7fa9 cell \u7684\u8996\u89ba\u6548\u679c\n        return cell\n    }\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u63d0\u793a<\/h2>\n<p>\n\u5728\u4f7f\u7528 UICollectionView \u7684\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u672a\u986f\u793a\u8cc7\u6599\uff1a<\/strong>\u78ba\u4fdd\u5df2\u6b63\u78ba\u8a2d\u7f6e <code>delegate<\/code> \u548c <code>dataSource<\/code>\uff0c\u4e26\u5728 <code>viewDidLoad<\/code> \u65b9\u6cd5\u4e2d\u9032\u884c\u521d\u59cb\u5316\u3002<\/li>\n<li><strong>cell \u91cd\u8907\u4f7f\u7528\u932f\u8aa4\uff1a<\/strong>\u78ba\u4fdd\u5df2\u8a3b\u518a cell\uff0c\u4e26\u5728 <code>cellForItemAt<\/code> \u65b9\u6cd5\u4e2d\u4f7f\u7528\u6b63\u78ba\u7684\u91cd\u7528\u8b58\u5225\u78bc\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528\u6848\u4f8b<\/h2>\n<p>\n\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u81ea\u5b9a\u7fa9 UICollectionView\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684 UICollectionViewCell \u985e\u5225\u4f86\u986f\u793a\u4e0d\u540c\u7684\u5167\u5bb9\u548c\u6a23\u5f0f\u3002<\/li>\n<li>\u5be6\u73fe\u4e0d\u540c\u7684\u5e03\u5c40\uff08\u5982\u683c\u5b50\u72c0\u6216\u7011\u5e03\u6d41\u5e03\u5c40\uff09\uff0c\u4ee5\u9069\u61c9\u4e0d\u540c\u7684\u986f\u793a\u9700\u6c42\u3002<\/li>\n<li>\u7d50\u5408 <code>UICollectionViewCompositionalLayout<\/code> \u4f86\u5275\u5efa\u66f4\u8907\u96dc\u7684\u4f48\u5c40\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\nSwift UICollectionView \u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5275\u5efa\u9ad8\u5ea6\u81ea\u5b9a\u7fa9\u7684\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\u3002\u901a\u904e\u7c21\u55ae\u7684\u8a2d\u7f6e\u548c\u5be6\u4f5c\uff0c\u4f60\u53ef\u4ee5\u5728\u61c9\u7528\u4e2d\u8f15\u9b06\u5c55\u793a\u8cc7\u6599\uff0c\u4e26\u4e14\u6839\u64da\u9700\u8981\u9032\u884c\u9032\u4e00\u6b65\u7684\u81ea\u5b9a\u7fa9\u3002\n<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u81ea\u5b9a\u7fa9 UICollectionViewCell\uff1f<\/h3>\n<p>\n\u4f60\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684 UICollectionViewCell \u985e\u5225\uff0c\u4e26\u5728\u8a72\u985e\u5225\u4e2d\u6dfb\u52a0\u4f60\u9700\u8981\u7684 UI \u5143\u7d20\uff0c\u4f8b\u5982\u6a19\u7c64\u548c\u5716\u7247\uff0c\u7136\u5f8c\u5728 <code>cellForItemAt<\/code> \u65b9\u6cd5\u4e2d\u9032\u884c\u914d\u7f6e\u3002\n<\/p>\n<h3>2. UICollectionView \u7684\u6027\u80fd\u512a\u5316\u6709\u4ec0\u9ebc\u5efa\u8b70\uff1f<\/h3>\n<p>\n\u70ba\u4e86\u512a\u5316\u6027\u80fd\uff0c\u78ba\u4fdd\u5728 cellForItemAt \u4e2d\u91cd\u7528 cells\uff0c\u4e26\u5118\u91cf\u6e1b\u5c11\u5728\u8a72\u65b9\u6cd5\u4e2d\u7684\u8a08\u7b97\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>UICollectionViewCompositionalLayout<\/code> \u4f86\u512a\u5316\u4f48\u5c40\u6027\u80fd\u3002\n<\/p>\n<h3>3. UICollectionView \u652f\u6301\u54ea\u4e9b\u5e03\u5c40\uff1f<\/h3>\n<p>\nUICollectionView \u652f\u6301\u591a\u7a2e\u5e03\u5c40\uff0c\u5305\u62ec <code>UICollectionViewFlowLayout<\/code>\u3001<code>UICollectionViewCompositionalLayout<\/code>\uff0c\u4ee5\u53ca\u81ea\u5b9a\u7fa9\u5e03\u5c40\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9748\u6d3b\u9078\u64c7\u3002\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UICollectionView-\ud83c\udfa8\u81ea\u5b9a\u7fa9\u5340\u584a\uff01_1675401439.854375.webp\" alt=\"Swift UICollectionView \ud83c\udfa8\u81ea\u5b9a\u7fa9\u5340\u584a\uff01\" title=\"Swift UICollectionView\u3001\u81ea\u5b9a\u7fa9\u5340\u584a\u3001UICollectionView\u57fa\u672c\u6982\u5ff5\u3001\u8996\u89ba\u6548\u679c\u3001\u793a\u4f8b\" width=\"1200\" height=\"628\"\/><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftUICollectionView\u4f86\u81ea\u5b9a\u7fa9\u5340\u584a\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5275\u5efa\u51fa\u7368\u7279\u7684\u8996\u89ba\u6548\u679c\u3002\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0eUICollectionView\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u793a\u4f8b\u4f86\u8b93\u4f60\u958b\u59cb\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"author":1,"featured_media":7049,"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-7050","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-UICollectionView-\ud83c\udfa8\u81ea\u5b9a\u7fa9\u5340\u584a\uff01_1675401439.854375.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1PI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7050","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=7050"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7050\/revisions"}],"predecessor-version":[{"id":13418,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7050\/revisions\/13418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7049"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}