{"id":6776,"date":"2025-06-02T13:03:21","date_gmt":"2025-06-05T05:21:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6776"},"modified":"2025-06-05T13:03:21","modified_gmt":"2025-06-05T05:21:20","slug":"https-badgameshow-com-steven-64","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-64\/","title":{"rendered":"Swift UICollectionView \u4f7f\u7528\u6307\u5357 \ud83c\udfa8 &#8211; 2025 \u6700\u65b0\u52d5\u614b\u8868\u683c\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UICollectionView, \u52d5\u614b\u8868\u683c\u6559\u5b78\"><\/p>\n<h1>Swift UICollectionView \u4f7f\u7528\u6307\u5357 \ud83c\udfa8 &#8211; 2025 \u6700\u65b0\u52d5\u614b\u8868\u683c\u6559\u5b78<\/h1>\n<p>UICollectionView \u662f iOS \u958b\u767c\u4e2d\u5e38\u7528\u7684\u63a7\u4ef6\uff0c\u80fd\u8b93\u958b\u767c\u8005\u5feb\u901f\u5efa\u7acb\u52d5\u614b\u8868\u683c\uff0c\u63d0\u5347\u4f7f\u7528\u8005\u67e5\u770b\u8cc7\u6599\u7684\u4fbf\u5229\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5efa\u7acb UICollectionView\uff0c\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u53ca\u5ef6\u4f38\u61c9\u7528\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u8fc5\u901f\u4e0a\u624b\u4e26\u638c\u63e1\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5efa\u7acb UICollectionView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b UICollectionView\uff0c\u4e26\u8a2d\u5b9a\u5176\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb UICollectionView \u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 320, height: 480), collectionViewLayout: UICollectionViewFlowLayout())\ncollectionView.backgroundColor = UIColor.white\n<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u8a2d\u5b9a UICollectionView \u7684\u6a23\u5f0f\uff0c\u4f8b\u5982\u6bcf\u500b cell \u7684\u5927\u5c0f\u548c section \u7684\u9593\u8ddd\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout\nlayout.itemSize = CGSize(width: 100, height: 100)\nlayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)\nlayout.minimumLineSpacing = 10\nlayout.minimumInteritemSpacing = 10\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a UICollectionView \u7684 DataSource<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u8a2d\u5b9a UICollectionView \u7684 <strong>DataSource<\/strong>\uff0c\u4ee5\u63d0\u4f9b\u8cc7\u6599\u4f86\u6e90\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">collectionView.dataSource = self\n<\/code><\/pre>\n<p>\u5be6\u4f5c UICollectionViewDataSource \u7684\u5fc5\u8981\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func numberOfSections(in collectionView: UICollectionView) -> Int {\n    return 1\n}\n\nfunc collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n    return 10\n}\n\nfunc collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"Cell\", for: indexPath)\n    cell.backgroundColor = UIColor.red\n    return cell\n}\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a UICollectionView \u7684 Delegate<\/h2>\n<p>\u6700\u5f8c\uff0c\u8a2d\u5b9a UICollectionView \u7684 <strong>Delegate<\/strong>\uff0c\u4ee5\u4fbf\u5728\u4f7f\u7528\u8005\u9ede\u64ca cell \u6642\u4f5c\u51fa\u53cd\u61c9\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">collectionView.delegate = self\n<\/code><\/pre>\n<p>\u5be6\u4f5c UICollectionViewDelegate \u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {\n    print(\"You tapped cell number \\(indexPath.row).\")\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u5be6\u4f5c UICollectionView \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec cell \u7121\u6cd5\u986f\u793a\u6216\u8cc7\u6599\u4e0d\u6b63\u78ba\u3002\u78ba\u4fdd\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u6aa2\u67e5 delegate \u548c dataSource \u662f\u5426\u6b63\u78ba\u8a2d\u5b9a\u3002<\/li>\n<li>\u78ba\u4fdd cell \u91cd\u7528\u6a19\u8b58\u7b26\u8207 dequeueReusableCell \u7684\u6a19\u8b58\u7b26\u4e00\u81f4\u3002<\/li>\n<li>\u5728 layoutSubviews \u4e2d\u9069\u7576\u8abf\u6574 UICollectionView \u7684 frame\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UICollectionView \u9664\u4e86\u53ef\u4ee5\u7528\u4f86\u986f\u793a\u5716\u7247\u548c\u6587\u5b57\u5916\uff0c\u9084\u53ef\u4ee5\u5be6\u4f5c\u66f4\u8907\u96dc\u7684\u4f48\u5c40\uff0c\u4f8b\u5982\u7011\u5e03\u6d41\u5e03\u5c40\u3001\u7db2\u683c\u5e03\u5c40\u7b49\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u81ea\u8a02 UICollectionViewLayout\uff0c\u5be6\u73fe\u5404\u7a2e\u5275\u610f\u7684\u8996\u89ba\u6548\u679c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UICollectionView\u4f7f\u7528\ud83c\udfa8-\u52d5\u614b\u8868\u683c\u6559\u5b78_1675395508.995209.webp\" alt=\"Swift UICollectionView \u4f7f\u7528\u6307\u5357\" title=\"Swift UICollectionView\u3001\u52d5\u614b\u8868\u683c\u3001\u7a0b\u5f0f\u8a9e\u6cd5\u3001\u8868\u683c\u5275\u5efa\u3001\u958b\u767c\u8005\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UICollectionView \u8207 UITableView \u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>A1: UICollectionView \u63d0\u4f9b\u66f4\u9748\u6d3b\u7684\u4f48\u5c40\u9078\u64c7\uff0c\u9069\u5408\u5c55\u793a\u591a\u6a23\u5316\u7684\u5167\u5bb9\uff0c\u800c UITableView \u5247\u9069\u5408\u986f\u793a\u55ae\u4e00\u985e\u578b\u7684\u5217\u8868\u8cc7\u6599\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728 UICollectionView \u4e2d\u986f\u793a\u5716\u7247\uff1f<\/h3>\n<p>A2: \u5728 cellForItemAt \u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u5c07 UIImageView \u52a0\u5165 cell \u4e2d\uff0c\u4e26\u8a2d\u7f6e\u5176\u5716\u7247\u3002<\/p>\n<h3>Q3: UICollectionView \u7684\u6027\u80fd\u512a\u5316\u5efa\u8b70\u6709\u54ea\u4e9b\uff1f<\/h3>\n<p>A3: \u4f7f\u7528 cell \u91cd\u7528\u6a5f\u5236\u3001\u5118\u91cf\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u4f48\u5c40\u8a08\u7b97\u3001\u5229\u7528\u9810\u52a0\u8f09\u6280\u8853\u7b49\u90fd\u80fd\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>&#8220;`<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\u5275\u5efa\u52d5\u614b\u8868\u683c\uff0c\u4e26\u8a73\u7d30\u4ecb\u7d39UICollectionView\u7684\u7a0b\u5f0f\u8a9e\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5275\u5efa\u51fa\u81ea\u5df1\u60f3\u8981\u7684\u8868\u683c\u3002<\/p>\n","protected":false},"author":1,"featured_media":6775,"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-6776","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\u4f7f\u7528\ud83c\udfa8-\u52d5\u614b\u8868\u683c\u6559\u5b78_1675395508.995209.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Li","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6776","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=6776"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6776\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6775"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}