{"id":410,"date":"2025-05-31T10:53:10","date_gmt":"2025-06-05T03:28:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=410"},"modified":"2025-06-05T10:53:10","modified_gmt":"2025-06-05T03:28:10","slug":"swift-tableview-%e5%ae%a2%e8%a3%bd%e5%8c%96-cell","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-tableview-%e5%ae%a2%e8%a3%bd%e5%8c%96-cell\/","title":{"rendered":"Swift 2025 \u6559\u5b78\uff1a\u5982\u4f55\u5ba2\u88fd\u5316 UITableView Cell"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>Swift 2025 \u6559\u5b78\uff1a\u5982\u4f55\u5ba2\u88fd\u5316 UITableView Cell<\/h1>\n<h2>\u70ba\u4ec0\u9ebc\u8981\u5ba2\u88fd\u5316 UITableView Cell<\/h2>\n<p>\u5728\u958b\u767c iOS \u61c9\u7528\u6642\uff0c\u7576\u7f8e\u8853\u8a2d\u8a08\u63d0\u4f9b\u7684\u7248\u578b\u7121\u6cd5\u4f7f\u7528 UITableView \u7684\u5167\u5efa cell \u4f86\u5448\u73fe\u6642\uff0c\u60a8\u5c31\u9700\u8981\u5ba2\u88fd\u5316 UITableView Cell\u3002\u5ba2\u88fd\u5316\u7684\u65b9\u5f0f\u53ef\u4ee5\u662f\u900f\u904e\u7a0b\u5f0f\u78bc\u6216\u4f7f\u7528 Xib \u6587\u4ef6\u9032\u884c\u8a2d\u8a08\u3002\u672c\u7bc7\u6587\u7ae0\u5c07\u5c08\u6ce8\u65bc\u5982\u4f55\u4f7f\u7528 Xib \u6587\u4ef6\u4f86\u9032\u884c UITableView Cell \u7684\u5ba2\u88fd\u5316\u3002<\/p>\n<h2>\u7c21\u8981\u6982\u8ff0<\/h2>\n<p>\u9019\u7bc7\u6559\u5b78\u5c07\u6703\u5e36\u60a8\u5f9e\u6700\u57fa\u672c\u7684\u5ba2\u88fd\u5316 cell \u958b\u59cb\uff0c\u6700\u7d42\u9054\u6210\u4e00\u500b\u5305\u542b\u591a\u7a2e\u5143\u7d20\u7684 UITableView\u3002\u4ee5\u4e0b\u662f\u6211\u5011\u5c07\u6703\u5be6\u4f5c\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190922\/20112271INCRiBUdqP.jpg\" alt=\"Swift tableview \u5ba2\u88fd\u5316 cell\" title=\"\"><\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u5728\u4e0d\u540c\u7684 index row \u4e2d\u586b\u5145 banner\u3001collectionView \u4ee5\u53ca\u666e\u901a\u7684\u5ba2\u88fd\u5316 UITableView Cell\u3002<\/p>\n<h2>1. \u65b0\u589e UITableView \u5ba2\u88fd\u5316 Cell<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u65b0\u589e\u4e00\u500b Swift + Xib \u6587\u4ef6\uff0c\u9078\u64c7 UITableView Cell \u4f5c\u70ba\u985e\u578b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190922\/20112271Fs0zCMh4fD.png\" alt=\"Swift tableview \u5ba2\u88fd\u5316 cell\" title=\"\"><\/p>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u7684\u8a2d\u8a08\u5c07\u6703\u5305\u542b\u4e00\u500b\u5c01\u9762\u5716\u7247\uff0c\u5716\u7247\u7684\u9ad8\u5ea6\u7d04\u70ba cell \u9ad8\u5ea6\u7684\u4e00\u534a\uff0c\u5176\u9918\u7684\u6587\u5b57\u5c07\u5e73\u5747\u5206\u914d\u81f3\u4e0b\u534a\u90e8\u3002<\/p>\n<h2>2. \u8a2d\u8a08 Cell \u7684 UI<\/h2>\n<p>\u5728 Xib \u6587\u4ef6\u4e2d\uff0c\u8a2d\u7f6e\u60a8\u7684 UI \u5143\u7d20\uff0c\u5305\u62ec\u5716\u7247\u548c\u6587\u672c\u6a19\u7c64\uff0c\u4e26\u78ba\u4fdd\u5b83\u5011\u7684\u7d04\u675f\uff08Constraints\uff09\u6b63\u78ba\u914d\u7f6e\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u87a2\u5e55\u4e0a\u986f\u793a\u826f\u597d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190922\/20112271sNwbKVaCPp.png\" alt=\"Swift tableview \u5ba2\u88fd\u5316 cell\" title=\"\"><\/p>\n<h2>3. \u65b0\u589e UITableView<\/h2>\n<p>\u5728\u60a8\u7684 ViewController \u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u521d\u59cb\u5316\u548c\u914d\u7f6e UITableView\uff1a<\/p>\n<p>&#8220;`swift<br \/>\noverride func initIMUI() {<br \/>\n    super.initIMUI()<br \/>\n    self.view.backgroundColor = UIColor.white<br \/>\n    self.view.addSubview(self.collectionView)<br \/>\n    self.view.addSubview(self.tableView)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u8a2d\u5b9a tableView \u7684\u4f4d\u7f6e\uff0c\u78ba\u4fdd\u5b83\u7dca\u8cbc\u5728 collectionView \u4e0b\u65b9\u3002\u8a08\u7b97 y \u503c\u6642\uff0c\u60a8\u9700\u8981\u8003\u616e\u5230 collectionView \u7684\u6700\u5927 y \u503c\u548c tabBar \u7684\u9ad8\u5ea6\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlazy var tableView: UITableView = {<br \/>\n    let tableView = UITableView()<br \/>\n    tableView.frame = CGRect(x: 20, y: collectionView.frame.maxY, width: KScreenWidth &#8211; 20 * 2, height: KScreenHeight &#8211; collectionView.frame.maxY &#8211; kTabBarHeight)<br \/>\n    tableView.delegate = self<br \/>\n    tableView.dataSource = self<br \/>\n    tableView.register(UINib(nibName: &#8220;CoverCell&#8221;, bundle: nil), forCellReuseIdentifier: &#8220;myCell&#8221;)<br \/>\n    return tableView<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h2>4. \u8a3b\u518a\u5ba2\u88fd\u5316 Cell<\/h2>\n<p>\u78ba\u4fdd\u60a8\u7684 ViewController \u7e7c\u627f UITableViewDelegate \u548c UITableViewDataSource\uff0c\u4e26\u5be6\u4f5c\u5fc5\u8981\u7684\u51fd\u6578\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nclass HomeViewController: JGBaseViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDelegate, UITableViewDataSource {<br \/>\n    \/\/ &#8230;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>5. \u5be6\u4f5c UITableView Data Source \u65b9\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5fc5\u9808\u5be6\u4f5c\u7684 Data Source \u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\n\/\/ MARK: &#8211; UITableViewDataSource<br \/>\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {<br \/>\n    return 10 \/\/ \u8fd4\u56de\u884c\u6578<br \/>\n}<\/p>\n<p>func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {<br \/>\n    return 300 \/\/ \u6bcf\u4e00\u884c\u7684\u9ad8\u5ea6<br \/>\n}<\/p>\n<p>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {<br \/>\n    let cell = tableView.dequeueReusableCell(withIdentifier: &#8220;myCell&#8221;, for: indexPath) as! CoverCell<br \/>\n    \/\/ \u5728\u9019\u88e1\u914d\u7f6e cell<br \/>\n    return cell<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u60a8\u7684\u5ba2\u88fd\u5316 cell \u5c31\u57fa\u672c\u5b8c\u6210\u4e86\uff0c\u63a5\u4e0b\u4f86\u60a8\u53ef\u4ee5\u9032\u4e00\u6b65\u8abf\u6574 UI \u4ee5\u9054\u5230\u7406\u60f3\u6548\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/TsM5yLF.gif\" alt=\"Swift tableview \u5ba2\u88fd\u5316 cell\" title=\"\"><\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5df2\u7d93\u5b78\u6703\u5982\u4f55\u5ba2\u88fd\u5316 UITableView Cell\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u5347\u60a8\u61c9\u7528\u7684\u5916\u89c0\uff0c\u9084\u80fd\u589e\u52a0\u4f7f\u7528\u8005\u9ad4\u9a57\u3002\u5225\u5fd8\u4e86\u6301\u7e8c\u512a\u5316\u60a8\u7684\u8a2d\u8a08\uff0c\u8b93\u5b83\u66f4\u7b26\u5408\u4f7f\u7528\u8005\u9700\u6c42\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u70ba\u4ec0\u9ebc\u8981\u4f7f\u7528 Xib \u6587\u4ef6\u4f86\u5ba2\u88fd\u5316 UITableView Cell\uff1f<\/h3>\n<p>A: \u4f7f\u7528 Xib \u6587\u4ef6\u53ef\u4ee5\u8b93\u60a8\u66f4\u76f4\u89c0\u5730\u8a2d\u8a08 UI\uff0c\u4e26\u4e14\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u76f8\u540c\u7684 cell \u8a2d\u8a08\uff0c\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u91cd\u8907\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u89e3\u6c7a UITableView Cell \u986f\u793a\u4e0d\u6b63\u78ba\u7684\u554f\u984c\uff1f<\/h3>\n<p>A: \u78ba\u4fdd\u60a8\u7684\u7d04\u675f\u8a2d\u5b9a\u6b63\u78ba\uff0c\u4e26\u6aa2\u67e5 cell \u7684\u91cd\u7528\u6a19\u8b58\u7b26\u662f\u5426\u6b63\u78ba\u8a2d\u5b9a\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u63d0\u9ad8 UITableView \u7684\u6027\u80fd\uff1f<\/h3>\n<p>A: \u4f7f\u7528\u9810\u5148\u8a08\u7b97\u7684 cell \u9ad8\u5ea6\uff0c\u5be6\u4f5c\u61f6\u52a0\u8f09\u6280\u8853\uff0c\u4e26\u78ba\u4fdd\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u7e6a\u64cd\u4f5c\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u88e1\u9762\u7684\u69cb\u9020\u6975\u5176\u8907\u96dc<br \/>\n\u5728\u4e0d\u540c\u7684`index row`\u88e1\u9762<br \/>\n\u6709\u585e `banner`<br \/>\n\u6709\u585e `collectionview`<br \/>\n\u4ee5\u53ca \u666e\u901a\u7684\u5ba2\u88fd\u5316 `tableview cell`<br \/>\n\u4eca\u5929\u5c31\u8981\u5f9e\u6700\u7c21\u55ae\u7684\u5ba2\u88fd\u5316`cell`\u5148\u505a\u8d77<\/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-410","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-6C","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/410","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=410"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":7579,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/410\/revisions\/7579"}],"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=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}