{"id":7420,"date":"2025-05-30T14:00:36","date_gmt":"2025-06-05T06:12:35","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7420"},"modified":"2025-06-05T14:00:36","modified_gmt":"2025-06-05T06:12:35","slug":"https-badgameshow-com-steven-381","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-381\/","title":{"rendered":"Swift 2025 \u6700\u65b0\u7248 UICollectionView \u591a\u500b\u5340\u584a\u5e03\u5c40\u5b8c\u5168\u6307\u5357"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UICollectionView, \u5340\u584a\u5e03\u5c40, iOS \u958b\u767c, Swift 2025\"><\/p>\n<h2>\u7c21\u4ecb<\/h2>\n<p>\u5728iOS\u958b\u767c\u4e2d\uff0c<strong>UICollectionView<\/strong>\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u8b93\u6211\u5011\u5275\u5efa\u8907\u96dc\u7684\u7528\u6236\u754c\u9762\u3002\u672c\u6587\u5c07\u70ba\u60a8\u63d0\u4f9b\u5982\u4f55\u4f7f\u7528 <strong>Swift 2025<\/strong> \u4f86\u5275\u5efa\u591a\u500b\u5340\u584a\u5e03\u5c40\u7684\u8a73\u7d30\u6b65\u9a5f\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u60a8\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u53ef\u7528\u6027\u548c\u7f8e\u89c0\u6027\u3002<\/p>\n<h2>\u5275\u5efa UICollectionView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b UICollectionView \u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u5275\u5efa\u4e00\u500b\u57fa\u672c\u7684 UICollectionView\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), collectionViewLayout: UICollectionViewFlowLayout())<\/code><\/pre>\n<h2>\u8a2d\u7f6e UICollectionView \u7684\u5e03\u5c40<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u8a2d\u7f6e UICollectionView \u7684\u5e03\u5c40\u4ee5\u652f\u6301\u591a\u500b\u5340\u584a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let layout = UICollectionViewFlowLayout()\nlayout.itemSize = CGSize(width: 100, height: 100)\nlayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)\nlayout.minimumInteritemSpacing = 10\nlayout.minimumLineSpacing = 10\ncollectionView.collectionViewLayout = layout<\/code><\/pre>\n<h2>\u5275\u5efa\u591a\u500b\u5340\u584a\u5e03\u5c40<\/h2>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UICollectionViewCompositionalLayout<\/strong> \u4f86\u5275\u5efa\u591a\u500b\u5340\u584a\u5e03\u5c40\uff0c\u4ee5\u4e0b\u662f\u5275\u5efa\u5169\u500b\u5340\u584a\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let section1 = NSCollectionLayoutSection(\n    group: NSCollectionLayoutGroup.horizontal(\n        layoutSize: .init(widthDimension: .fractionalWidth(1.0),\n                          heightDimension: .absolute(100)),\n        subitems: [\n            NSCollectionLayoutItem(\n                layoutSize: .init(widthDimension: .fractionalWidth(0.5),\n                                  heightDimension: .fractionalHeight(1.0)),\n                contentInsets: .init(top: 0, leading: 0, bottom: 0, trailing: 10)\n            ),\n            NSCollectionLayoutItem(\n                layoutSize: .init(widthDimension: .fractionalWidth(0.5),\n                                  heightDimension: .fractionalHeight(1.0)),\n                contentInsets: .init(top: 0, leading: 10, bottom: 0, trailing: 0)\n            )\n        ]\n    )\n)\n\nlet section2 = NSCollectionLayoutSection(\n    group: NSCollectionLayoutGroup.vertical(\n        layoutSize: .init(widthDimension: .fractionalWidth(1.0),\n                          heightDimension: .absolute(100)),\n        subitems: [\n            NSCollectionLayoutItem(\n                layoutSize: .init(widthDimension: .fractionalWidth(1.0),\n                                  heightDimension: .fractionalHeight(0.5)),\n                contentInsets: .init(top: 0, leading: 0, bottom: 10, trailing: 0)\n            ),\n            NSCollectionLayoutItem(\n                layoutSize: .init(widthDimension: .fractionalWidth(1.0),\n                                  heightDimension: .fractionalHeight(0.5)),\n                contentInsets: .init(top: 10, leading: 0, bottom: 0, trailing: 0)\n            )\n        ]\n    )\n)\n\nlet layout = UICollectionViewCompositionalLayout { (sectionIndex, layoutEnvironment) -> NSCollectionLayoutSection? in\n    switch sectionIndex {\n    case 0:\n        return section1\n    case 1:\n        return section2\n    default:\n        return nil\n    }\n}\n\ncollectionView.collectionViewLayout = layout<\/code><\/pre>\n<h2>\u6dfb\u52a0 UICollectionView \u5230\u8996\u5716<\/h2>\n<p>\u6700\u5f8c\uff0c\u5c07 UICollectionView \u6dfb\u52a0\u5230\u8996\u5716\u4e2d\uff0c\u4ee5\u4fbf\u986f\u793a\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">view.addSubview(collectionView)<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u554f\u984c\uff0c\u4f8b\u5982\u5e03\u5c40\u4e0d\u6b63\u78ba\u6216\u8005\u5143\u7d20\u7121\u6cd5\u6b63\u78ba\u986f\u793a\u3002\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u8a8d UICollectionView \u7684 frame \u8a2d\u7f6e\u6b63\u78ba\u3002<\/li>\n<li>\u6aa2\u67e5\u6bcf\u500b\u5340\u584a\u7684\u5c3a\u5bf8\u548c\u9593\u8ddd\u8a2d\u7f6e\u3002<\/li>\n<li>\u78ba\u4fdd\u60a8\u5df2\u7d93\u70ba UICollectionView \u8a2d\u7f6e\u9069\u7576\u7684\u6578\u64da\u6e90\u548c\u59d4\u8a17\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f7f\u7528 UICollectionView \u7684\u591a\u500b\u5340\u584a\u5e03\u5c40\uff0c\u53ef\u4ee5\u5275\u5efa\u66f4\u5177\u52d5\u614b\u6027\u7684\u754c\u9762\uff0c\u4f8b\u5982\uff1a\u793e\u4ea4\u5a92\u9ad4\u61c9\u7528\u7684\u5716\u7247\u6d41\u3001\u5546\u54c1\u5c55\u793a\u9801\u9762\u7b49\u3002\u96a8\u8457\u9700\u6c42\u7684\u8b8a\u5316\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u7528\u4f8b\u9748\u6d3b\u8abf\u6574\u5e03\u5c40\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UICollectionView\u7684\u591a\u500b\u5340\u584a\u5e03\u5c40_1675475683.751173.webp\" alt=\"Swift UICollectionView\u7684\u591a\u500b\u5340\u584a\u5e03\u5c40\" title=\"Swift UICollectionView\u3001\u591a\u500b\u5340\u584a\u5e03\u5c40\u3001iOS\u958b\u767c\u3001\u6392\u5217\u5143\u7d20\u3001\u7f8e\u89c0\u61c9\u7528\u7a0b\u5f0f\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u8abf\u6574 UICollectionView \u7684\u884c\u9593\u8ddd\u548c\u5217\u9593\u8ddd\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e UICollectionViewFlowLayout \u7684 <code>minimumLineSpacing<\/code> \u548c <code>minimumInteritemSpacing<\/code> \u5c6c\u6027\u4f86\u8abf\u6574\u884c\u9593\u8ddd\u548c\u5217\u9593\u8ddd\u3002<\/p>\n<h3>Q2: UICollectionView \u7684\u6027\u80fd\u512a\u5316\u6709\u4ec0\u9ebc\u5efa\u8b70\uff1f<\/h3>\n<p>A2: \u4f7f\u7528 <code>dequeueReusableCell<\/code> \u65b9\u6cd5\u4f86\u91cd\u7528\u55ae\u5143\u683c\uff0c\u4e26\u4e14\u76e1\u91cf\u6e1b\u5c11\u5728 <code>cellForItemAt<\/code> \u4e2d\u7684\u8a08\u7b97\u548c\u6578\u64da\u8655\u7406\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h3>Q3: UICollectionView \u652f\u6301\u54ea\u4e9b\u52d5\u756b\u6548\u679c\uff1f<\/h3>\n<p>A3: UICollectionView \u652f\u6301\u591a\u7a2e\u52d5\u756b\u6548\u679c\uff0c\u5982\u63d2\u5165\u3001\u522a\u9664\u548c\u91cd\u8f09\u884c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>performBatchUpdates<\/code> \u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e9b\u52d5\u756b\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2SwiftUICollectionView\u7684\u591a\u500b\u5340\u584a\u5e03\u5c40\uff0c\u53ef\u4ee5\u8b93\u4f60\u5728iOS\u958b\u767c\u4e2d\u66f4\u6709\u6548\u7387\u5730\u6392\u5217\u548c\u7ba1\u7406\u4f60\u7684\u5143\u7d20\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u7f8e\u89c0\u3002<\/p>\n","protected":false},"author":1,"featured_media":7419,"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-7420","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\u7684\u591a\u500b\u5340\u584a\u5e03\u5c40_1675475683.751173.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1VG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7420","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=7420"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7420\/revisions"}],"predecessor-version":[{"id":13577,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7420\/revisions\/13577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7419"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}