{"id":408,"date":"2025-06-03T10:35:49","date_gmt":"2025-06-05T03:27:48","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=408"},"modified":"2025-06-05T10:35:49","modified_gmt":"2025-06-05T03:27:48","slug":"swift-collectionview-%e5%ae%a2%e8%a3%bd%e5%8c%96-cell","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-collectionview-%e5%ae%a2%e8%a3%bd%e5%8c%96-cell\/","title":{"rendered":"2025 \u6700\u65b0 Swift CollectionView \u5ba2\u88fd\u5316 Cell \u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<h1>2025 \u6700\u65b0 Swift CollectionView \u5ba2\u88fd\u5316 Cell \u5b8c\u6574\u6307\u5357<\/h1>\n<h2>\u70ba\u4ec0\u9ebc\u8981\u5ba2\u88fd\u5316 Cell<\/h2>\n<p>\u5728\u8a31\u591a\u60c5\u5883\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5ba2\u88fd\u5316 CollectionView \u7684 Cell \u4ee5\u7b26\u5408\u8a2d\u8a08\u9700\u6c42\u3002\u82e5\u7f8e\u8853\u63d0\u4f9b\u7684\u5716\u6a94\u7121\u6cd5\u76f4\u63a5\u4f7f\u7528\u5167\u5efa\u7684 Cell\uff0c\u60a8\u5c07\u9700\u8981\u9032\u884c\u5ba2\u88fd\u5316\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6559\u60a8\u5982\u4f55\u4f7f\u7528 Xib \u4f86\u5be6\u73fe\u9019\u4e00\u904e\u7a0b\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>1. \u65b0\u589e CollectionView \u5ba2\u88fd\u5316 Cell + Xib<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190921\/201122715A5MKGYSSt.png\" alt=\"\u65b0\u589e CollectionView \u5ba2\u88fd\u5316 Cell + Xib\" title=\"\"><br \/>\n*\u793a\u610f\u5716\uff1a\u65b0\u589e CollectionView \u5ba2\u88fd\u5316 Cell \u7684\u6b65\u9a5f*<\/p>\n<h2>2. \u8a2d\u5b9a\u60a8\u6240\u9700\u7684\u6a23\u5f0f<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190921\/201122718dpo6sCs8i.png\" alt=\"\u8a2d\u5b9a\u60a8\u6240\u9700\u7684\u6a23\u5f0f\" title=\"\"><br \/>\n*\u793a\u610f\u5716\uff1a\u8a2d\u5b9a CollectionView Cell \u7684\u6a23\u5f0f*<\/p>\n<h2>3. \u5ba3\u544a Cell \u7269\u4ef6<\/h2>\n<p>\u5728\u60a8\u7684 Xib \u6a94\u6848\u4e2d\uff0c\u4f7f\u7528 IBOutlet \u5ba3\u544a Cell \u7684\u5143\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\"> \n@IBOutlet var iconImg: UIImageView!\n@IBOutlet var textLabel: UILabel!\n<\/code><\/pre>\n<h2>4. \u65b0\u589e CollectionView<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728\u60a8\u60f3\u8981\u65b0\u589e CollectionView \u7684\u5730\u65b9\uff0c\u4f7f\u7528 addSubview \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\"> \noverride func viewDidLoad() {\n    super.viewDidLoad()\n    self.view.addSubview(self.collectionView)\n}\n<\/code><\/pre>\n<p>\u9019\u88e1\u4f7f\u7528\u7684\u662f\u61f6\u52a0\u8f09\uff08lazy loading\uff09\u4f86\u521d\u59cb\u5316 CollectionView\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\"> \nlazy var collectionView: UICollectionView = {\n    let layout = UICollectionViewFlowLayout()\n    layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)\n    layout.minimumLineSpacing = 5\n    layout.itemSize = CGSize(width: 100, height: 30)\n    layout.scrollDirection = .horizontal\n    \n    let collectionView = UICollectionView(frame: CGRect(x: 0, y: kSafeTopPadding + kNavBarHeight, width: UIScreen.main.bounds.width, height: 50), collectionViewLayout: layout)\n    collectionView.backgroundColor = UIColor.gray\n    collectionView.register(UINib(nibName: \"HomeCell\", bundle: nil), forCellWithReuseIdentifier: \"myCell\")\n    collectionView.delegate = self\n    collectionView.dataSource = self\n\n    return collectionView\n}()\n<\/code><\/pre>\n<h2>5. \u52a0\u5165 CollectionView Delegate \u548c DataSource<\/h2>\n<p>\u78ba\u4fdd\u60a8\u7684 ViewController \u7e7c\u627f UICollectionViewDataSource \u548c UICollectionViewDelegate\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\"> \nclass HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {\n    \/\/ Implement delegate methods here\n}\n<\/code><\/pre>\n<h2>6. \u5be6\u4f5c CollectionView \u7684 Delegate \u65b9\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u662f\u9700\u8981\u5be6\u4f5c\u7684\u4e3b\u8981 Delegate \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\"> \n\/\/ MARK: - CollectionView DataSource Methods\nfunc collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {\n    return 10\n}\n\nfunc collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n    let cell: HomeCell = collectionView.dequeueReusableCell(withReuseIdentifier: \"myCell\", for: indexPath) as! HomeCell\n    cell.backgroundColor = UIColor.lightGray\n    cell.clipsToBounds = true\n    cell.layer.cornerRadius = cell.frame.height \/ 2\n    cell.textLabel.text = \"\u5206\u985e\"\n    cell.textLabel.textColor = UIColor.black\n    cell.iconImg?.image = UIImage(named: \"icBuyrecordArrowdwn\")\n    cell.iconImg?.backgroundColor = .clear\n    return cell\n}\n\nfunc numberOfSections(in collectionView: UICollectionView) -> Int {\n    return 2\n}\n\nfunc collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {\n    print(\"\u4f60\u9078\u64c7\u4e86\u7b2c \\(indexPath.section + 1) \u7d44\u7684\u7b2c \\(indexPath.item + 1) \u500b\u9805\u76ee\")\n}\n<\/code><\/pre>\n<p>\u5728 cellForItemAt \u65b9\u6cd5\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6839\u64da indexPath \u70ba\u6bcf\u500b Cell \u8a2d\u5b9a\u4e0d\u540c\u7684\u6a23\u5f0f\u8207\u5167\u5bb9\u3002<\/p>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/EeKCsU4.gif\" alt=\"Swift CollectionView \u5ba2\u88fd\u5316 Cell Demo\" title=\"\"><br \/>\n*\u793a\u610f\u5716\uff1aSwift CollectionView \u5ba2\u88fd\u5316 Cell \u7684\u5be6\u4f5c\u6548\u679c*<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift CollectionView \u5ba2\u88fd\u5316 Cell\" title=\"\"><\/a><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. CollectionView \u548c TableView \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>CollectionView \u662f\u7528\u4f86\u5c55\u793a\u591a\u7dad\u8cc7\u6599\u7684\u5143\u4ef6\uff0c\u901a\u5e38\u7528\u65bc\u986f\u793a\u5716\u7247\u6216\u7db2\u683c\u5167\u5bb9\uff0c\u800c TableView \u5247\u662f\u7528\u4f86\u5c55\u793a\u55ae\u7dad\u8cc7\u6599\u7684\u5217\u8868\u3002<\/p>\n<h3>2. \u5982\u4f55\u6539\u5584 CollectionView \u6ed1\u52d5\u7684\u6027\u80fd\uff1f<\/h3>\n<p>\u78ba\u4fdd\u4f7f\u7528\u9069\u7576\u7684 Cell \u91cd\u7528\u6a5f\u5236\uff0c\u4e26\u76e1\u91cf\u6e1b\u5c11 Cell \u4e2d\u7684\u7e6a\u88fd\u64cd\u4f5c\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u6ed1\u52d5\u6027\u80fd\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u5728 CollectionView \u4e2d\u4f7f\u7528\u4e0d\u540c\u7684 Cell \u985e\u578b\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u500b CollectionView \u4e2d\u8a3b\u518a\u591a\u500b Cell \u985e\u578b\uff0c\u4e26\u6839\u64da\u9700\u6c42\u5728 cellForItemAt \u65b9\u6cd5\u4e2d\u9032\u884c\u5224\u65b7\u8207\u8a2d\u5b9a\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73fe\u5728\u8981\u5f9e\u982d\u5230\u5c3e\u958b\u59cb\u6642\u505a<br \/>\n\u4e0a\u65b9\u7684\u7be9\u9078`Bar`\u4e0d\u6703\u96a8\u8457\u4e0b\u9762`tableview`\u79fb\u52d5<br \/>\n\u6211\u731c\u6e2c\u61c9\u8a72\u4e0d\u662f`tableview`\u7684`header`<br \/>\n\u6240\u4ee5\u61c9\u8a72\u53ef\u4ee5\u55ae\u7d14\u65b9\u4e00\u500b`collectionview`\u5728\u4e0a\u9762<br \/>\n\u4eca\u5929\u5c31\u8981\u4f86\u6642\u505a`collectionview`\u4ee5\u53ca\u5ba2\u88fd\u5316`cell`<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-6A","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/408","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=408"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/408\/revisions\/983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}