{"id":6708,"date":"2025-06-04T12:02:58","date_gmt":"2025-06-05T05:00:57","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6708"},"modified":"2025-06-05T12:02:58","modified_gmt":"2025-06-05T05:00:57","slug":"https-badgameshow-com-steven-32","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-32\/","title":{"rendered":"Swift UICollectionView \u5b8c\u6574\u6559\u5b78\uff1a\u6253\u9020\u4f60\u7684\u6536\u96c6\u904a\u6232 \ud83d\udc8e\ud83d\udcb0\uff082025 \u6700\u65b0\u8a9e\u6cd5\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UICollectionView, iOS \u958b\u767c, \u6536\u96c6\u904a\u6232, \u6559\u5b78\"><\/p>\n<h2>Swift UICollectionView \u7c21\u4ecb<\/h2>\n<p>\u5728iOS\u958b\u767c\u4e2d\uff0cUICollectionView\u662f\u4e00\u500b\u6975\u70ba\u91cd\u8981\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u4e14\u9748\u6d3b\u5730\u5efa\u7acb\u51fa\u7f8e\u89c0\u7684\u7528\u6236\u4ecb\u9762\u3002\u672c\u6587\u5c07\u4ee52025\u6700\u65b0\u7684Swift\u8a9e\u6cd5\u4f86\u4ecb\u7d39\u5982\u4f55\u5be6\u73feUICollectionView\uff0c\u4e26\u4ee5\u6253\u9020\u4e00\u500b\u7c21\u55ae\u7684\ud83d\udc8e\ud83d\udcb0\u6536\u96c6\u904a\u6232\u70ba\u4f8b\u3002<\/p>\n<h2>\u5275\u5efa UICollectionView<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500bUICollectionView\uff0c\u4e26\u5c07\u5b83\u6dfb\u52a0\u5230\u6211\u5011\u7684\u8996\u5716\u63a7\u5236\u5668\u4e2d\u3002\u5728Swift\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u5b8c\u6210\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let layout = UICollectionViewFlowLayout()\nlet collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)\ncollectionView.backgroundColor = .white\nview.addSubview(collectionView)\n<\/code><\/pre>\n<h2>\u8a2d\u7f6e DataSource \u548c Delegate<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6eUICollectionView\u7684<strong>dataSource<\/strong>\u548c<strong>delegate<\/strong>\uff0c\u4ee5\u4fbf\u6211\u5011\u53ef\u4ee5\u63a7\u5236UICollectionView\u7684\u884c\u70ba\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">collectionView.dataSource = self\ncollectionView.delegate = self\n<\/code><\/pre>\n<h2>\u5be6\u73fe UICollectionViewDataSource \u548c UICollectionViewDelegate<\/h2>\n<p>\u70ba\u4e86\u63a7\u5236UICollectionView\u7684\u884c\u70ba\uff0c\u6211\u5011\u9700\u8981\u5be6\u73feUICollectionViewDataSource\u548cUICollectionViewDelegate\u5354\u8b70\u3002\u4ee5\u4e0b\u662f\u5177\u9ad4\u7684\u5be6\u4f5c\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {\n    \/\/ MARK: - UICollectionViewDataSource\n    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n        return 10 \/\/ \u9019\u88e1\u53ef\u4ee5\u6839\u64da\u4f60\u7684\u6578\u64da\u6e90\u9032\u884c\u8abf\u6574\n    }\n\n    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: \"Cell\", for: indexPath)\n        \/\/ \u5728\u9019\u88e1\u8a2d\u7f6ecell\u7684\u5167\u5bb9\n        return cell\n    }\n\n    \/\/ MARK: - UICollectionViewDelegate\n    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {\n        \/\/ \u8655\u7406\u9078\u64c7\u4e8b\u4ef6\n    }\n}\n<\/code><\/pre>\n<h2>\u5275\u5efa UICollectionViewCell<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684UICollectionViewCell\uff0c\u4ee5\u4fbf\u5c07\u5176\u6dfb\u52a0\u5230UICollectionView\u4e2d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class CollectionViewCell: UICollectionViewCell {\n    let imageView = UIImageView()\n\n    override init(frame: CGRect) {\n        super.init(frame: frame)\n        contentView.addSubview(imageView)\n    }\n\n    required init?(coder aDecoder: NSCoder) {\n        fatalError(\"init(coder:) has not been implemented\")\n    }\n\n    override func layoutSubviews() {\n        super.layoutSubviews()\n        imageView.frame = contentView.bounds\n    }\n}\n<\/code><\/pre>\n<h2>\u8a2d\u7f6e UICollectionView \u7684 Cell<\/h2>\n<p>\u5728UICollectionViewDataSource\u7684cellForItemAt\u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u8a2d\u7f6e\u6bcf\u500bcell\u7684\u5716\u50cf\u548c\u5167\u5bb9\uff0c\u4e26\u5728UICollectionViewDelegate\u7684didSelectItemAt\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u904a\u6232\u908f\u8f2f\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u82e5\u5728\u4f7f\u7528UICollectionView\u6642\u9047\u5230\u554f\u984c\uff0c\u53ef\u4ee5\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u5df2\u7d93\u6b63\u78ba\u8a2d\u7f6edataSource\u548cdelegate\u3002<\/li>\n<li>\u6aa2\u67e5cell\u7684\u91cd\u7528\u8b58\u5225\u7b26\u662f\u5426\u4e00\u81f4\u3002<\/li>\n<li>\u78ba\u8a8dUICollectionView\u7684frame\u53calayout\u914d\u7f6e\u6b63\u78ba\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f7f\u7528UICollectionView\u53ef\u4ee5\u5be6\u73fe\u8a31\u591a\u4e0d\u540c\u7684\u61c9\u7528\uff0c\u5982\u76f8\u518a\u700f\u89bd\u3001\u5546\u54c1\u5c55\u793a\u7b49\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u81ea\u8eab\u9700\u6c42\u9032\u884c\u64f4\u5c55\uff0c\u6dfb\u52a0\u66f4\u591a\u7684\u7528\u6236\u4ea4\u4e92\u529f\u80fd\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UICollectionView-\u5be6\u73fe-\ud83d\udc8e\ud83d\udcb0_1675394274.5079181.webp\" alt=\"Swift UICollectionView \u5be6\u73fe \ud83d\udc8e\ud83d\udcb0\" title=\"Swift UICollectionView, \ud83d\udc8e\ud83d\udcb0, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewFlowLayout\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UICollectionView\u548cUITableView\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>A1: UICollectionView\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9748\u6d3b\u6027\uff0c\u5141\u8a31\u958b\u767c\u8005\u81ea\u5b9a\u7fa9\u6392\u5217\u65b9\u5f0f\uff0c\u800cUITableView\u5247\u662f\u55ae\u5217\u7684\u5782\u76f4\u6efe\u52d5\u5217\u8868\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u81ea\u5b9a\u7fa9UICollectionView\u7684layout\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u901a\u904e\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684UICollectionViewLayout\u985e\u4f86\u5be6\u73fe\u81ea\u5b9a\u7fa9\u5e03\u5c40\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u7684UICollectionView\u66f4\u5177\u500b\u6027\u5316\u3002<\/p>\n<h3>Q3: UICollectionView\u5982\u4f55\u8655\u7406\u591a\u7a2ecell\u985e\u578b\uff1f<\/h3>\n<p>A3: \u4f60\u53ef\u4ee5\u5728cellForItemAt\u65b9\u6cd5\u4e2d\u6839\u64daindexPath\u7684\u4e0d\u540c\u8fd4\u56de\u4e0d\u540c\u985e\u578b\u7684cell\uff0c\u4e26\u5728UICollectionView\u7684\u6570\u636e\u6e90\u4e2d\u63d0\u4f9b\u76f8\u61c9\u7684\u6578\u64da\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528SwiftUICollectionView\u4f86\u5be6\u73fe\u7cbe\u7f8e\u7684\ud83d\udc8e\ud83d\udcb0\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528UICollectionViewDelegate\u548cUICollectionViewDataSource\u4f86\u69cb\u5efa\u81ea\u5b9a\u7fa9\u7684\ud83d\udc8e\ud83d\udcb0\uff0c\u4e26\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528UICollectionViewFlowLayout\u4f86\u63a7\u5236\ud83d\udc8e\ud83d\udcb0\u7684\u4f48\u5c40\u3002<\/p>\n","protected":false},"author":1,"featured_media":6707,"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-6708","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-\u5be6\u73fe-\ud83d\udc8e\ud83d\udcb0_1675394274.5079181.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Kc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6708","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=6708"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6708\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6707"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}