{"id":6750,"date":"2025-05-31T13:09:53","date_gmt":"2025-06-05T05:15:52","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6750"},"modified":"2025-06-05T13:09:53","modified_gmt":"2025-06-05T05:15:52","slug":"https-badgameshow-com-steven-51","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-51\/","title":{"rendered":"Swift UITableView \u5237\u65b0\u529f\u80fd\u5be6\u4f5c\uff1a\u4f7f\u7528 UIRefreshControl \u7684\u6700\u65b0\u6559\u5b78\uff082025 \u66f4\u65b0\u7248\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITableView, UIRefreshControl, iOS \u958b\u767c, \u5237\u65b0\u529f\u80fd\"><\/p>\n<h1>Swift UITableView \u5237\u65b0\u529f\u80fd\u5be6\u4f5c\uff1a\u4f7f\u7528 UIRefreshControl \u7684\u6700\u65b0\u6559\u5b78\uff082025 \u66f4\u65b0\u7248\uff09<\/h1>\n<p>UITableView \u662f iOS \u958b\u767c\u4e2d\u6700\u5e38\u7528\u7684\u63a7\u4ef6\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u5efa\u7acb\u4e00\u500b\u5217\u8868\u3002\u70ba\u4e86\u63d0\u5347\u4f7f\u7528\u8005\u9ad4\u9a57\uff0c\u6211\u5011\u7d93\u5e38\u9700\u8981\u5728\u5217\u8868\u4e2d\u52a0\u5165\u5237\u65b0\u529f\u80fd\uff0c\u9019\u6642\u5019\u5c31\u53ef\u4ee5\u5229\u7528 <strong>UIRefreshControl<\/strong> \u4f86\u5be6\u73fe\u3002<\/p>\n<p>UIRefreshControl \u662f\u4e00\u500b\u53ef\u4ee5\u8b93\u4f7f\u7528\u8005\u62c9\u52d5\u5217\u8868\u4e26\u89f8\u767c\u5237\u65b0\u7684\u63a7\u4ef6\uff0c\u9019\u4f7f\u5f97\u4f7f\u7528\u8005\u80fd\u5920\u8f15\u9b06\u66f4\u65b0\u5217\u8868\u4e2d\u7684\u8cc7\u6599\u3002\u5b83\u7684\u4f7f\u7528\u4e5f\u76f8\u7576\u7c21\u55ae\uff0c\u53ea\u9700\u5e7e\u884c\u7a0b\u5f0f\u78bc\u5373\u53ef\u5b8c\u6210\u8a2d\u7f6e\u3002<\/p>\n<h2>\u5be6\u4f5c\u6b65\u9a5f<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 UITableView \u4e2d\u52a0\u5165 UIRefreshControl \u7684\u8a73\u7d30\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u5ba3\u544a UIRefreshControl \u7269\u4ef6<\/h3>\n<pre><code class=\"language-swift line-numbers\">let refreshControl = UIRefreshControl()<\/code><\/pre>\n<h3>2. \u5c07 UIRefreshControl \u52a0\u5165 UITableView<\/h3>\n<pre><code class=\"language-swift line-numbers\">tableView.addSubview(refreshControl)<\/code><\/pre>\n<h3>3. \u8a2d\u5b9a target \u548c action \u4ee5\u89f8\u767c\u5237\u65b0<\/h3>\n<pre><code class=\"language-swift line-numbers\">refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)<\/code><\/pre>\n<h3>4. \u5be6\u4f5c\u5237\u65b0\u8cc7\u6599\u7684\u908f\u8f2f<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5728 target \u7684\u65b9\u6cd5\u4e2d\u5be6\u4f5c\u66f4\u65b0\u8cc7\u6599\u7684\u908f\u8f2f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func refreshData() {\n    \/\/ \u6a21\u64ec\u8cc7\u6599\u66f4\u65b0\n    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {\n        \/\/ \u66f4\u65b0\u8cc7\u6599\n        self.tableView.reloadData()\n        self.refreshControl.endRefreshing()\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 <code>DispatchQueue.main.asyncAfter<\/code> \u4f86\u6a21\u64ec\u8cc7\u6599\u66f4\u65b0\u7684\u5ef6\u9072\uff0c\u8b93\u4f7f\u7528\u8005\u80fd\u5920\u770b\u5230\u5237\u65b0\u52d5\u756b\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 UIRefreshControl \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u5237\u65b0\u5f8c\u8cc7\u6599\u672a\u66f4\u65b0\uff1a<\/strong>\u78ba\u4fdd\u5728 <code>refreshData()<\/code> \u65b9\u6cd5\u4e2d\u6b63\u78ba\u66f4\u65b0\u8cc7\u6599\u4e26\u547c\u53eb <code>reloadData()<\/code>\u3002<\/li>\n<li><strong>UIRefreshControl \u4e0d\u986f\u793a\uff1a<\/strong>\u6aa2\u67e5\u662f\u5426\u6b63\u78ba\u5c07 <code>refreshControl<\/code> \u52a0\u5165\u5230 <code>tableView<\/code> \u4e2d\uff0c\u4e26\u78ba\u8a8d target \u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86 UITableView\uff0cUIRefreshControl \u4e5f\u53ef\u4ee5\u7528\u65bc <strong>UICollectionView<\/strong>\uff0c\u9019\u5728\u9700\u8981\u5728\u7db2\u683c\u4e2d\u5be6\u73fe\u5237\u65b0\u529f\u80fd\u6642\u975e\u5e38\u6709\u7528\u3002\u8acb\u53c3\u8003\u4ee5\u4e0b\u9023\u7d50\u4ee5\u4e86\u89e3\u66f4\u591a\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.hackingwithswift.com\/example-code\/uikit\/how-to-add-a-refresh-control-to-a-collection-view\" target=\"_blank\" rel=\"noopener\">\u5982\u4f55\u5728\u96c6\u5408\u8996\u5716\u4e2d\u52a0\u5165\u5237\u65b0\u63a7\u4ef6<\/a><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView\u5237\u65b0\uff1aUIRefreshControl-\ud83d\ude80_1675395087.076361.webp\" alt=\"Swift UITableView\u5237\u65b0\uff1aUIRefreshControl \ud83d\ude80\" title=\"Swift UITableView\u5237\u65b0\u3001UIRefreshControl\u3001Swift\u8a9e\u8a00\u3001UITableView\u5237\u65b0\u6280\u8853\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UIRefreshControl \u53ef\u4ee5\u5728 iOS 15 \u4ee5\u4e0a\u7684\u7248\u672c\u4e2d\u4f7f\u7528\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0cUIRefreshControl \u5728 iOS \u7684\u6240\u6709\u7248\u672c\u4e2d\u5747\u53ef\u4f7f\u7528\uff0c\u4f46\u5728 iOS 15 \u4e4b\u5f8c\u6703\u6709\u66f4\u597d\u7684\u652f\u63f4\u53ca\u529f\u80fd\u3002<\/p>\n<h3>2. \u5982\u4f55\u81ea\u5b9a\u7fa9 UIRefreshControl \u7684\u5916\u89c0\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e <code>attributedTitle<\/code> \u6216\u4fee\u6539\u80cc\u666f\u984f\u8272\u4f86\u81ea\u5b9a\u7fa9 UIRefreshControl \u7684\u5916\u89c0\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u5728 UITableView \u7684\u4e0b\u65b9\u4f7f\u7528 UIRefreshControl \u55ce\uff1f<\/h3>\n<p>\u901a\u5e38 UIRefreshControl \u662f\u8a2d\u7f6e\u5728 UITableView \u7684\u4e0a\u65b9\u4f7f\u7528\u4ee5\u89f8\u767c\u4e0b\u62c9\u5237\u65b0\uff0c\u82e5\u9700\u8981\u5728\u4e0b\u65b9\u5be6\u73fe\u985e\u4f3c\u529f\u80fd\uff0c\u53ef\u80fd\u9700\u8981\u8003\u616e\u81ea\u5b9a\u7fa9\u63a7\u4ef6\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4e2d\u7684UIRefreshControl\u4f86\u5be6\u73feUITableView\u7684\u5237\u65b0\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u638c\u63e1UITableView\u7684\u5237\u65b0\u6280\u8853\u3002<\/p>\n","protected":false},"author":1,"featured_media":6749,"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-6750","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-UITableView\u5237\u65b0\uff1aUIRefreshControl-\ud83d\ude80_1675395087.076361.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1KS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6750","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=6750"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6750\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6749"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}