{"id":419,"date":"2025-05-30T10:30:02","date_gmt":"2025-06-05T03:30:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=419"},"modified":"2025-06-05T10:30:02","modified_gmt":"2025-06-05T03:30:01","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%8b","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%8b\/","title":{"rendered":"Swift TableView \u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\u8207\u9802\u90e8\u5716\u7247\u7684\u5b8c\u6574\u6307\u5357\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<h3>\u7c21\u8981<\/h3>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u5728 Swift \u4e2d\u5be6\u73fe\u4e00\u500b\u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\u7684\u6548\u679c\uff0c\u4e26\u8b93\u9802\u90e8\u5716\u7247\u96a8\u8457\u6efe\u52d5\u800c\u8b8a\u5316\u3002\u6211\u5011\u5c07\u4f7f\u7528 `UIScrollViewDelegate` \u7684 `scrollViewDidScroll` \u65b9\u6cd5\u4f86\u9054\u6210\u9019\u500b\u6548\u679c\uff0c\u4e26\u5c55\u793a\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u61c9\u7528\u5728\u81ea\u5df1\u7684\u5c08\u6848\u4e2d\u3002\u9019\u7a2e\u6548\u679c\u5728\u8a31\u591a\u61c9\u7528\u4e2d\u90fd\u80fd\u770b\u5230\uff0c\u4f8b\u5982\u539f\u751f\u7684 Uber Eats \u61c9\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/Z5Zv1Fd.gif\" alt=\"\u4e0b\u62c9\u653e\u5927\u5c01\u9762\u7167\u6548\u679c\u793a\u7bc4\" title=\"\"><\/p>\n<h3>\u57fa\u672c\u8a2d\u7f6e<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u7f6e `UITableView` \u548c\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684 `topView` \u4f86\u986f\u793a\u4e00\u4e9b\u9910\u5ef3\u7684\u8cc7\u8a0a\u3002\u4ee5\u4e0b\u662f\u8a2d\u7f6e\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\noverride func viewDidLoad() {<br \/>\n    super.viewDidLoad()<br \/>\n    self.view.addSubview(self.tableView)<br \/>\n    self.view.addSubview(self.topView)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u6703\u5b9a\u7fa9\u4e00\u500b `topView`\uff0c\u9019\u500b\u8996\u5716\u5c07\u5305\u542b\u6a19\u984c\u3001\u5206\u985e\u548c\u5176\u4ed6\u8cc7\u8a0a\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlazy var topView: UIView = {<br \/>\n    let topView = UIView()<br \/>\n    topView.frame = CGRect(x: 15, y: 100, width: UIScreen.main.bounds.width &#8211; 30, height: 200)<br \/>\n    topView.backgroundColor = UIColor.white<\/p>\n<p>    let titleLabel = UILabel()<br \/>\n    titleLabel.frame = CGRect(x: 20, y: 25, width: topView.frame.width &#8211; 40, height: 75)<br \/>\n    titleLabel.font = UIFont.systemFont(ofSize: 23)<br \/>\n    titleLabel.numberOfLines = 2<br \/>\n    titleLabel.text = &#8220;\u9ea5\u7576\u52de S161\u4e2d\u5fa9\u8208\u4e8c McDonald&#8217;s Fu Sing II, Taichung&#8221;<br \/>\n    topView.addSubview(titleLabel)<\/p>\n<p>    let stateLabel = UILabel()<br \/>\n    stateLabel.frame = CGRect(x: 20, y: titleLabel.frame.maxY, width: topView.frame.width &#8211; 40, height: 20)<br \/>\n    stateLabel.font = UIFont.systemFont(ofSize: 13)<br \/>\n    stateLabel.text = &#8220;\u7f8e\u5f0f\u7f8e\u98df\u30fb$&#8221;<br \/>\n    topView.addSubview(stateLabel)<\/p>\n<p>    let otherLabel = UILabel()<br \/>\n    otherLabel.frame = CGRect(x: 20, y: stateLabel.frame.maxY + 20, width: topView.frame.width &#8211; 40, height: 20)<br \/>\n    otherLabel.textColor = UIColor.darkGray<br \/>\n    otherLabel.font = UIFont.systemFont(ofSize: 13)<br \/>\n    otherLabel.text = &#8220;5-15\u5206\u9418 4.8(500+) 15.00TWD \u8cbb\u7528&#8221;<br \/>\n    topView.addSubview(otherLabel)<\/p>\n<p>    return topView<br \/>\n}()<br \/>\n&#8220;`<\/p>\n<h3>scrollViewDidScroll \u65b9\u6cd5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5be6\u73fe `scrollViewDidScroll` \u65b9\u6cd5\u4f86\u8655\u7406\u6efe\u52d5\u4e8b\u4ef6\uff0c\u8b93 `topView` \u548c\u5c01\u9762\u7167\u96a8\u8457\u6efe\u52d5\u9032\u884c\u8b8a\u5316\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc scrollViewDidScroll(_ scrollView: UIScrollView) {<br \/>\n    let offsetY = scrollView.contentOffset.y<br \/>\n    let radius = -offsetY \/ 200<\/p>\n<p>    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    }<\/p>\n<p>    let topViewX: CGFloat = 15 * radius<br \/>\n    topView.frame = CGRect(x: topViewX, y: 100 &#8211; 200 &#8211; offsetY, width: UIScreen.main.bounds.width &#8211; topViewX * 2, height: 200)<\/p>\n<p>    \/\/ \u900f\u660e\u5ea6\u548c\u6a19\u984c\u884c\u6578\u7684\u8abf\u6574<br \/>\n    if radius > 0 {<br \/>\n        titleLabel.numberOfLines = 2<br \/>\n        stateLabel.alpha = radius<br \/>\n        otherLabel.alpha = radius<br \/>\n    } else {<br \/>\n        titleLabel.numberOfLines = 1<br \/>\n        stateLabel.alpha = 0<br \/>\n        otherLabel.alpha = 0<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u6839\u64da\u6ed1\u52d5\u8ddd\u96e2\u8abf\u6574 `topView` \u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff0c\u4e26\u63a7\u5236\u6a19\u984c\u7684\u900f\u660e\u5ea6\u548c\u884c\u6578\uff0c\u4f7f\u5176\u96a8\u8457\u6efe\u52d5\u6548\u679c\u800c\u8b8a\u5316\u3002<\/p>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/TuznjNC.gif\" alt=\"\u4e0b\u62c9\u653e\u5927\u6548\u679c\u6f14\u793a\" title=\"\"><\/p>\n<h3>\u5b8c\u6210\u6548\u679c<\/h3>\n<p>\u6700\u7d42\u6548\u679c\u5c07\u6703\u662f\u7576\u7528\u6236\u4e0b\u62c9\u6642\uff0c\u5e95\u90e8\u7684\u5c01\u9762\u7167\u6703\u653e\u5927\uff0c\u4e26\u4e14 `topView` \u7684\u5167\u5bb9\u6703\u96a8\u8457\u6efe\u52d5\u800c\u8b8a\u5316\uff0c\u5be6\u73fe\u4e00\u500b\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/KBqmKK0.gif\" alt=\"\u5b8c\u6210\u6548\u679c\u5c55\u793a\" title=\"\"><\/p>\n<blockquote><p>\n  GitHub Repository: <a href=\"https:\/\/github.com\/Bgihe\/TableViewHeaderTest\" target=\"_blank\" rel=\"noopener\">TableViewHeaderTest<\/a>\n<\/p><\/blockquote>\n<hr \/>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<p>**Q1: \u5982\u4f55\u8abf\u6574\u5c01\u9762\u7167\u7684\u5927\u5c0f\uff1f**<br \/>\nA1: \u4f60\u53ef\u4ee5\u8abf\u6574 `headImageView` \u7684\u521d\u59cb\u6846\u67b6\u6216\u5728 `scrollViewDidScroll` \u65b9\u6cd5\u4e2d\u66f4\u6539\u7e2e\u653e\u56e0\u5b50\u4f86\u6539\u8b8a\u5c01\u9762\u7167\u7684\u5927\u5c0f\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6539\u8b8a\u6a19\u984c\u7684\u5b57\u9ad4\u548c\u984f\u8272\uff1f**<br \/>\nA2: \u5728 `titleLabel` \u7684\u914d\u7f6e\u4e2d\uff0c\u4f7f\u7528 `UIFont.systemFont(ofSize: )` \u65b9\u6cd5\u4f86\u8a2d\u7f6e\u5b57\u9ad4\u5927\u5c0f\uff0c\u4f7f\u7528 `titleLabel.textColor` \u5c6c\u6027\u4f86\u8a2d\u7f6e\u984f\u8272\u3002<\/p>\n<p>**Q3: \u9019\u500b\u6548\u679c\u53ef\u4ee5\u61c9\u7528\u5728\u54ea\u4e9b\u5834\u5408\uff1f**<br \/>\nA3: \u9019\u500b\u4e0b\u62c9\u653e\u5927\u6548\u679c\u9069\u5408\u7528\u65bc\u9910\u5ef3\u3001\u65c5\u904a\u6216\u5176\u4ed6\u9700\u8981\u5c55\u793a\u5c01\u9762\u7167\u7247\u7684\u61c9\u7528\u4e2d\uff0c\u53ef\u4ee5\u63d0\u5347\u7528\u6236\u7684\u8996\u89ba\u9ad4\u9a57\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u512a\u5316\u4e0d\u50c5\u63d0\u5347\u4e86\u6587\u7ae0\u7684\u5b8c\u6574\u6027\u8207\u5be6\u7528\u6027\uff0c\u9084\u9075\u5faa\u4e86 SEO \u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u6587\u7ae0\u5728\u641c\u5c0b\u5f15\u64ce\u4e2d\u7372\u5f97\u66f4\u597d\u7684\u6392\u540d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u6b21\u505a\u5b8c\u4e0a\u9762\u7684\u5c01\u9762\u7167\u7247\u7684\u9023\u52d5<br \/>\n\u63a5\u4e0b\u4f86\u4e0b\u9762\u6709\u4e00\u500b\u7279\u6b8a\u7684`View`<br \/>\n\u4e0a\u6ed1\u5230\u4e00\u5b9a\u7a0b\u5ea6\u5f8c<br \/>\n\u5c31\u6703\u96a8\u8457\u8ddd\u96e2<br \/>\n\u6700\u5f8c\u8b8a\u6210`navigation bar`<\/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-419","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-6L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/419","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=419"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":570,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/419\/revisions\/570"}],"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=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}