{"id":7042,"date":"2025-05-30T14:35:39","date_gmt":"2025-06-05T07:13:38","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7042"},"modified":"2025-06-05T14:35:39","modified_gmt":"2025-06-05T07:13:38","slug":"https-badgameshow-com-steven-197","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-197\/","title":{"rendered":"Swift UICollectionView \u5b8c\u6574\u6559\u5b78\uff1a\u8f15\u9b06\u5e03\u5c40\u591a\u884c\u591a\u5217\u5143\u7d20\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09\ud83d\udcd0"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UICollectionView, \u5e03\u5c40, \u591a\u884c\u591a\u5217, Swift 2025, UICollectionView \u6559\u5b78\"><\/p>\n<h1>Swift UICollectionView \u5b8c\u6574\u6559\u5b78\uff1a\u8f15\u9b06\u5e03\u5c40\u591a\u884c\u591a\u5217\u5143\u7d20\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09\ud83d\udcd0<\/h1>\n<p>Swift UICollectionView \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u65bc\u8f15\u9b06\u5730\u5e03\u5c40\u591a\u884c\u591a\u5217\u7684\u5143\u7d20\uff0c\u8b93\u4f60\u7684 App \u5167\u5bb9\u66f4\u52a0\u7f8e\u89c0\u4e14\u5177\u6709\u4e92\u52d5\u6027\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift UICollectionView \u4f86\u5be6\u73fe\u591a\u884c\u591a\u5217\u7684\u5143\u7d20\u5e03\u5c40\uff0c\u4e26\u63d0\u4f9b\u8a73\u7d30\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u5efa\u8b70\u53ca\u5ef6\u4f38\u61c9\u7528\u7684\u60f3\u6cd5\u3002<\/p>\n<h2>\u5efa\u7acb UICollectionView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 Storyboard \u4e2d\u5efa\u7acb\u4e00\u500b UICollectionView\uff0c\u4e26\u5c07\u5b83\u62d6\u62c9\u5230 ViewController \u4e2d\u3002\u78ba\u4fdd\u4f60\u7684 ViewController \u7e7c\u627f\u81ea <code>UIViewController<\/code> \u4e26\u9075\u5faa <code>UICollectionViewDataSource<\/code> \u5354\u8b70\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UICollectionViewDataSource {\n    @IBOutlet weak var collectionView: UICollectionView!\n    \/\/ ...\n}\n<\/code><\/pre>\n<h2>\u5be6\u4f5c UICollectionViewDataSource \u65b9\u6cd5<\/h2>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u5be6\u4f5c <code>UICollectionViewDataSource<\/code> \u7684\u5169\u500b\u5fc5\u8981\u65b9\u6cd5\uff0c\u4ee5\u63d0\u4f9b UICollectionView \u6240\u9700\u7684\u8cc7\u6599\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n    \/\/ \u8fd4\u56de\u8981\u986f\u793a\u7684\u5143\u7d20\u6578\u91cf\n    return 10\n}\n\nfunc collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n    \/\/ \u8fd4\u56de\u8981\u986f\u793a\u7684 UICollectionViewCell\n    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"Cell\", for: indexPath)\n    \/\/ \u8a2d\u5b9a cell \u7684\u5167\u5bb9\n    return cell\n}\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a UICollectionView \u7684\u5e03\u5c40<\/h2>\n<p>\u6211\u5011\u9700\u8981\u8a2d\u5b9a UICollectionView \u7684\u5e03\u5c40\uff0c\u4ee5\u4fbf\u63a7\u5236\u5176\u591a\u884c\u591a\u5217\u7684\u6392\u5217\u3002\u53ef\u4ee5\u4f7f\u7528 <code>UICollectionViewFlowLayout<\/code> \u4f86\u8a2d\u5b9a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let layout = UICollectionViewFlowLayout()\nlayout.itemSize = CGSize(width: 100, height: 100)\nlayout.minimumLineSpacing = 10\nlayout.minimumInteritemSpacing = 10\ncollectionView.collectionViewLayout = layout\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u8a2d\u5b9a UICollectionView \u7684\u5143\u7d20\u5927\u5c0f\u70ba 100&#215;100\uff0c\u4e26\u8a2d\u7f6e\u884c\u9593\u8ddd\u548c\u5217\u9593\u8ddd\u70ba 10\uff0c\u4ee5\u4fbf\u6709\u6548\u5730\u63a7\u5236\u5143\u7d20\u7684\u6392\u5217\u3002<\/p>\n<h2>\u7d81\u5b9a\u8cc7\u6599\u6e90<\/h2>\n<p>\u6700\u5f8c\uff0c\u5c07 UICollectionView \u7684\u8cc7\u6599\u6e90\u8a2d\u70ba ViewController\uff0c\u4ee5\u4fbf UICollectionView \u53ef\u4ee5\u6b63\u78ba\u5730\u8b80\u53d6\u8cc7\u6599\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">collectionView.dataSource = self\n<\/code><\/pre>\n<p>\u5b8c\u6210\u4ee5\u4e0a\u6b65\u9a5f\u5f8c\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528 Swift UICollectionView \u4f86\u8f15\u9b06\u5730\u5e03\u5c40\u591a\u884c\u591a\u5217\u7684\u5143\u7d20\uff0c\u8b93\u4f60\u7684 App \u5167\u5bb9\u66f4\u5177\u7f8e\u611f\u8207\u529f\u80fd\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UICollectionView-\u8f15\u9b06\u5e03\u5c40-\u591a\u884c\u591a\u5217-\ud83d\udcd0_1675401268.187732.webp\" alt=\"Swift UICollectionView \u8f15\u9b06\u5e03\u5c40 \u591a\u884c\u591a\u5217 \ud83d\udcd0\" title=\"Swift UICollectionView\u3001\u591a\u884c\u591a\u5217\u5e03\u5c40\u3001Swift \u5e03\u5c40\u3001App \u8a2d\u8a08\u611f\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 UICollectionView \u4e2d\u81ea\u5b9a\u7fa9 cell \u7684\u5916\u89c0\uff1f<\/h3>\n<p>A1: \u4f60\u53ef\u4ee5\u7e7c\u627f <code>UICollectionViewCell<\/code>\uff0c\u4e26\u5728\u5176\u4e2d\u81ea\u5b9a\u7fa9\u8996\u5716\u5143\u7d20\u3002\u7136\u5f8c\u5728 <code>cellForItemAt<\/code> \u65b9\u6cd5\u4e2d\u5c07\u5176\u914d\u7f6e\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u8655\u7406 UICollectionView \u7684\u9ede\u64ca\u4e8b\u4ef6\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u4f7f\u7528 <code>collectionView(_:didSelectItemAt:)<\/code> \u65b9\u6cd5\u4f86\u8655\u7406\u9ede\u64ca\u4e8b\u4ef6\uff0c\u4e26\u5728\u6b64\u65b9\u6cd5\u4e2d\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<h3>Q3: UICollectionView \u7684\u6027\u80fd\u512a\u5316\u6709\u54ea\u4e9b\u5efa\u8b70\uff1f<\/h3>\n<p>A3: \u4f7f\u7528\u91cd\u7528\u6a5f\u5236\u3001\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u8996\u5716\u66f4\u65b0\u3001\u4f7f\u7528 <code>UICollectionViewDiffableDataSource<\/code> \u4f86\u7c21\u5316\u8cc7\u6599\u7ba1\u7406\u8207\u6e32\u67d3\u904e\u7a0b\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2SwiftUICollectionView\u5e03\u5c40\uff0c\u8f15\u9b06\u638c\u63e1\u591a\u884c\u591a\u5217\u5e03\u5c40\uff0c\u8b93\u4f60\u7684App\u5145\u6eff\u8a2d\u8a08\u611f\uff01\u4e86\u89e3SwiftUICollectionView\u5982\u4f55\u4f7f\u7528\uff0c\u4ee5\u53ca\u5982\u4f55\u8a2d\u8a08\u51fa\u591a\u884c\u591a\u5217\u7684\u5e03\u5c40\uff0c\u8b93\u4f60\u7684App\u5145\u6eff\u8a2d\u8a08\u611f\uff01<\/p>\n","protected":false},"author":1,"featured_media":7041,"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-7042","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-\u8f15\u9b06\u5e03\u5c40-\u591a\u884c\u591a\u5217-\ud83d\udcd0_1675401268.187732.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1PA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7042","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=7042"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7042\/revisions"}],"predecessor-version":[{"id":13561,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7042\/revisions\/13561"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7041"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}