{"id":417,"date":"2025-05-29T11:13:36","date_gmt":"2025-06-05T03:29:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=417"},"modified":"2025-06-05T11:13:36","modified_gmt":"2025-06-05T03:29:36","slug":"swift-tableview-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e5%b0%81%e9%9d%a2%e7%85%a7-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e9%a0%82%e9%83%a8%e5%9c%96%e7%89%87-%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-tableview-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e5%b0%81%e9%9d%a2%e7%85%a7-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e9%a0%82%e9%83%a8%e5%9c%96%e7%89%87-%e4%b8%8a\/","title":{"rendered":"Swift TableView \u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<h3>\u7c21\u8981<\/h3>\n<p>\u5728\u672c\u6559\u5b78\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u5728 Swift \u7684 `UITableView` \u4e2d\u5be6\u73fe\u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\u7684\u6548\u679c\u3002\u7576\u7528\u6236\u4e0b\u62c9\u6efe\u52d5\u6642\uff0c\u5c01\u9762\u5716\u7247\u6703\u96a8\u8457\u4e0b\u62c9\u7684\u5e45\u5ea6\u800c\u653e\u5927\uff0c\u985e\u4f3c\u65bc Uber Eats \u7684\u529f\u80fd\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u4e0d\u50c5\u63d0\u5347\u4e86\u7528\u6236\u9ad4\u9a57\uff0c\u4e5f\u8b93\u61c9\u7528\u770b\u8d77\u4f86\u66f4\u52a0\u7cbe\u7dfb\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/IHkStSX.gif\" alt=\"Swift TableView \u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\u6548\u679c\" title=\"\"><\/p>\n<h3>\u8a2d\u7f6e UITableView<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728\u8996\u5716\u4e2d\u6dfb\u52a0\u4e00\u500b `UITableView`\uff0c\u4e26\u5728\u5176\u4e0a\u65b9\u65b0\u589e\u4e00\u500b `UIImageView` \u4f5c\u70ba header\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f Swift \u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlazy var tableView: UITableView = {<br \/>\n    let tableView = UITableView()<br \/>\n    tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)<br \/>\n    tableView.delegate = self<br \/>\n    tableView.dataSource = self<br \/>\n    tableView.register(UINib(nibName: &#8220;CoverCell&#8221;, bundle: nil), forCellReuseIdentifier: &#8220;CoverCell&#8221;)<br \/>\n    tableView.addSubview(self.headImageView)<br \/>\n    tableView.contentInset = UIEdgeInsets(top: 400 &#8211; 200, left: 0, bottom: 0, right: 0)<br \/>\n    return tableView<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h3>\u8a2d\u7f6e headImageView<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e `headImageView`\uff0c\u4e26\u70ba\u5176\u6307\u5b9a\u5716\u7247\u548c\u5927\u5c0f\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlazy var headImageView: UIImageView = {<br \/>\n    let headImageView = UIImageView()<br \/>\n    headImageView.frame = CGRect(x: 0, y: -200, width: UIScreen.main.bounds.width, height: 200)<br \/>\n    headImageView.contentMode = .scaleAspectFill<br \/>\n    headImageView.image = UIImage(named: &#8220;testPhoto&#8221;) \/\/ \u8acb\u78ba\u4fdd\u5716\u7247\u5df2\u88ab\u6dfb\u52a0\u5230\u5c08\u6848\u4e2d<br \/>\n    headImageView.clipsToBounds = true<br \/>\n    return headImageView<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h3>UITableView Delegate \u65b9\u6cd5<\/h3>\n<p>\u5728\u9019\u500b\u90e8\u5206\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e `UITableView` \u7684\u57fa\u672c\u6578\u64da\u6e90\u65b9\u6cd5\u3002\u9019\u88e1\u6211\u5011\u53ea\u7c21\u55ae\u4ecb\u7d39\uff0c\u8a73\u7d30\u5167\u5bb9\u53ef\u53c3\u8003\u4e4b\u524d\u7684\u6587\u7ae0\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {<br \/>\n    return 7 \/\/ \u8fd4\u56de\u884c\u6578<br \/>\n}<\/p>\n<p>func numberOfSections(in tableView: UITableView) -> Int {<br \/>\n    return 1 \/\/ \u8fd4\u56de\u5340\u57df\u6578<br \/>\n}<\/p>\n<p>func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {<br \/>\n    return 300 \/\/ \u8fd4\u56de\u884c\u9ad8<br \/>\n}<\/p>\n<p>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {<br \/>\n    let cell: CoverCell = tableView.dequeueReusableCell(withIdentifier: &#8220;CoverCell&#8221;) as! CoverCell<br \/>\n    cell.numLabel.text = String(indexPath.item)<br \/>\n    return cell<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u76e3\u807d\u6efe\u52d5\u4e8b\u4ef6<\/h3>\n<p>\u6700\u95dc\u9375\u7684\u90e8\u5206\u662f\u5be6\u73fe `scrollViewDidScroll` \u65b9\u6cd5\uff0c\u9019\u6a23\u6211\u5011\u5c31\u80fd\u6839\u64da\u6efe\u52d5\u8ddd\u96e2\u4f86\u653e\u5927\u5c01\u9762\u5716\u7247\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc scrollViewDidScroll(_ scrollView: UIScrollView) {<br \/>\n    let offsetY = scrollView.contentOffset.y<br \/>\n    let radius = -offsetY \/ 200 \/\/ 200 \u662f headView \u7684\u9ad8\u5ea6<br \/>\n    if (-offsetY > 200) {<br \/>\n        headImageView.transform = CGAffineTransform(scaleX: radius, y: radius)<br \/>\n        var frame = headImageView.frame<br \/>\n        frame.origin.y = offsetY<br \/>\n        headImageView.frame = frame<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u8a2d\u7f6e\u5f8c\uff0c\u6211\u5011\u7684\u4e0b\u62c9\u653e\u5927\u6548\u679c\u5c31\u5b8c\u6210\u4e86\uff0c\u8b93\u6211\u5011\u4f86\u770b\u770b\u6548\u679c\u5982\u4f55\u3002<\/p>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/3CPsfQj.gif\" alt=\"Swift TableView \u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167 Demo\" title=\"\"><\/p>\n<blockquote><p>\n  GitHub \u4ee3\u78bc\u5be6\u73fe<br \/>\n  [\u9ede\u64ca\u67e5\u770b](https:\/\/github.com\/Bgihe\/TableViewHeaderTest)\n<\/p><\/blockquote>\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=\"\u76f8\u95dc\u5716\u7247\" title=\"\"><\/a><\/p>\n<p>https:\/\/wp.me\/pcFK27-6J  <\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"EzlfJLxXv1\"><p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/swift-tableview-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e5%b0%81%e9%9d%a2%e7%85%a7-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e9%a0%82%e9%83%a8%e5%9c%96%e7%89%87-%e4%b8%8a\/\">Swift TableView \u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"Swift TableView \u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10 &#8212;  \u5c0f\u7926\u5de5\u6316\u6316\u7926-Steven\" src=\"https:\/\/badgameshow.com\/steven\/swift\/swift-tableview-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e5%b0%81%e9%9d%a2%e7%85%a7-%e4%b8%8b%e6%8b%89%e6%94%be%e5%a4%a7%e9%a0%82%e9%83%a8%e5%9c%96%e7%89%87-%e4%b8%8a\/embed\/#?secret=93RCtLieH7#?secret=EzlfJLxXv1\" data-secret=\"EzlfJLxXv1\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<p><b>Q1: \u5982\u4f55\u81ea\u5b9a\u7fa9 header \u7684\u9ad8\u5ea6\uff1f<\/b><br \/>\nA1: \u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e `tableView.contentInset` \u7684\u503c\u4f86\u8abf\u6574 header \u7684\u9ad8\u5ea6\u3002<\/p>\n<p><b>Q2: \u5982\u4f55\u5be6\u73fe\u5716\u7247\u7684\u5e73\u6ed1\u904e\u6e21\u6548\u679c\uff1f<\/b><br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `UIView.animate` \u65b9\u6cd5\uff0c\u6839\u64da\u6efe\u52d5\u7684\u8ddd\u96e2\u4f86\u8abf\u6574\u5716\u7247\u7684\u5c6c\u6027\u3002<\/p>\n<p><b>Q3: \u5982\u679c\u5716\u7247\u4e0d\u986f\u793a\uff0c\u8a72\u600e\u9ebc\u8fa6\uff1f<\/b><br \/>\nA3: \u78ba\u4fdd\u5716\u7247\u540d\u7a31\u8207\u4ee3\u78bc\u4e2d\u7684\u540d\u7a31\u5339\u914d\uff0c\u4e26\u4e14\u5716\u7247\u5df2\u6b63\u78ba\u6dfb\u52a0\u5230\u5c08\u6848\u4e2d\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u501f\u4e0b\u4f86\u8981\u505a`tableView`\u4e0b\u62c9\u6642<br \/>\n`\u5c01\u9762\u7167`\u5716\u7247\u6703`\u653e\u5927`<br \/>\n\u96a8\u8457\u4e0b\u767c\u5e45\u5ea6\u8d8a\u5927<br \/>\n\u5716\u7247\u4e5f\u6703\u653e\u5f97\u66f4\u5927<br \/>\n\u53ef\u4ee5\u4f7f\u7528`tableView` \u914d\u5408`\u65b0\u589e`\u4e00\u500b `header`<br \/>\n\u5c31\u53ef\u4ee5\u5be6\u73fe<br \/>\n\u770b\u4e00\u4e0b`Uber eat`\u7684\u6548\u679c<\/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-417","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-6J","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/417","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=417"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/417\/revisions\/822"}],"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=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}