{"id":7513,"date":"2025-05-31T15:22:51","date_gmt":"2025-06-05T07:32:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7513"},"modified":"2025-06-05T15:22:51","modified_gmt":"2025-06-05T07:32:51","slug":"https-badgameshow-com-steven-331","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-331\/","title":{"rendered":"Swift \u5728 UITableView \u4e2d\u5be6\u73fe\u4e0b\u62c9\u5237\u65b0\u529f\u80fd | 2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITableView, UIRefreshControl, \u4e0b\u62c9\u5237\u65b0, iOS \u958b\u767c\"><\/p>\n<h1>Swift \u5728 UITableView \u4e2d\u5be6\u73fe\u4e0b\u62c9\u5237\u65b0\u529f\u80fd | 2025 \u6700\u65b0\u6559\u5b78<\/h1>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c<strong>UITableView<\/strong> \u662f\u4e00\u500b\u975e\u5e38\u5e38\u7528\u7684\u5143\u4ef6\uff0c\u5b83\u80fd\u8b93\u958b\u767c\u8005\u5feb\u901f\u5efa\u7acb\u5217\u8868\u3002\u7136\u800c\uff0c\u6709\u6642\u5019\u6211\u5011\u9700\u8981\u5728 UITableView \u4e2d\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0\u7684\u529f\u80fd\uff0c\u8b93\u4f7f\u7528\u8005\u80fd\u5920\u8f15\u9b06\u5730\u66f4\u65b0\u8cc7\u6599\u3002\u5728 Swift \u4e2d\uff0c\u53ef\u4ee5\u900f\u904e <strong>UIRefreshControl<\/strong> \u4f86\u5feb\u901f\u5be6\u73fe\u9019\u500b\u529f\u80fd\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u5efa\u7acb UITableView<\/h2>\n<p>\u9996\u5148\uff0c\u5728\u4f60\u7684 ViewController \u4e2d\u5efa\u7acb\u4e00\u500b UITableView\uff0c\u4e26\u8a2d\u7f6e\u5176\u57fa\u672c\u5c6c\u6027\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb UITableView \u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UITableViewDataSource {\n    var tableView: UITableView!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        tableView = UITableView(frame: self.view.bounds)\n        tableView.dataSource = self\n        self.view.addSubview(tableView)\n    }\n\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n        return 20 \/\/ \u8fd4\u56de\u8cc7\u6599\u6578\u91cf\n    }\n\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n        let cell = UITableViewCell(style: .default, reuseIdentifier: \"cell\")\n        cell.textLabel?.text = \"Row \\(indexPath.row)\"\n        return cell\n    }\n}<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u6dfb\u52a0 UIRefreshControl<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u8981\u5728 UITableView \u4e0a\u6dfb\u52a0 <strong>UIRefreshControl<\/strong>\uff0c\u8b93\u6211\u5011\u7684\u4f7f\u7528\u8005\u80fd\u5920\u5be6\u73fe\u4e0b\u62c9\u5237\u65b0\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let refreshControl = UIRefreshControl()\ntableView.refreshControl = refreshControl<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u8a2d\u7f6e\u5237\u65b0\u4e8b\u4ef6<\/h2>\n<p>\u70ba <strong>UIRefreshControl<\/strong> \u6dfb\u52a0\u4e8b\u4ef6\uff0c\u7576\u4f7f\u7528\u8005\u4e0b\u62c9\u6642\u89f8\u767c\u66f4\u65b0\u8cc7\u6599\u7684\u51fd\u5f0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)\n\n@objc func refreshData() {\n    \/\/ \u66f4\u65b0\u8cc7\u6599\u7684\u908f\u8f2f\n    \/\/ \u5047\u8a2d\u9019\u88e1\u9032\u884c\u7db2\u8def\u8acb\u6c42\u66f4\u65b0\u8cc7\u6599\n    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {\n        \/\/ \u6a21\u64ec\u8cc7\u6599\u66f4\u65b0\n        self.tableView.reloadData()\n        self.refreshControl.endRefreshing()\n    }\n}<\/code><\/pre>\n<h2>\u6b65\u9a5f\u56db\uff1a\u8a2d\u7f6e\u5237\u65b0\u63d0\u793a<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728 <strong>UIRefreshControl<\/strong> \u4e0a\u8a2d\u7f6e\u63d0\u793a\u6587\u5b57\uff0c\u8b93\u4f7f\u7528\u8005\u77e5\u9053\u4ed6\u5011\u6b63\u5728\u9032\u884c\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">refreshControl.attributedTitle = NSAttributedString(string: \"\u6b63\u5728\u66f4\u65b0\u8cc7\u6599...\")<\/code><\/pre>\n<p>\u73fe\u5728\uff0c\u4f60\u5df2\u7d93\u6210\u529f\u70ba UITableView \u5be6\u73fe\u4e86\u4e0b\u62c9\u5237\u65b0\u529f\u80fd\u3002\u7576\u4f7f\u7528\u8005\u4e0b\u62c9\u6642\uff0c\u5c07\u6703\u89f8\u767c\u6211\u5011\u7684\u4e8b\u4ef6\uff0c\u4e26\u4e14\u66f4\u65b0\u8cc7\u6599\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>\u5982\u4f55\u5728\u4e0b\u62c9\u5237\u65b0\u4e2d\u986f\u793a\u7db2\u8def\u8acb\u6c42\u7684\u7d50\u679c\uff1f<\/h3>\n<p>\u5728 <code>refreshData()<\/code> \u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u7db2\u8def\u8acb\u6c42\u7684\u908f\u8f2f\uff0c\u4e26\u5728\u6578\u64da\u66f4\u65b0\u5f8c\u91cd\u65b0\u8f09\u5165 UITableView \u7684\u6578\u64da\u3002<\/p>\n<h3>UIRefreshControl \u53ef\u4ee5\u5728\u5176\u4ed6\u5143\u4ef6\u4e2d\u4f7f\u7528\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c<strong>UIRefreshControl<\/strong> \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc UITableView\uff0c\u4e5f\u53ef\u4ee5\u7528\u65bc UICollectionView \u7b49\u5176\u4ed6\u6efe\u52d5\u5143\u4ef6\u3002<\/p>\n<h3>\u5982\u4f55\u81ea\u8a02 UIRefreshControl \u7684\u5916\u89c0\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4fee\u6539 <strong>UIRefreshControl<\/strong> \u7684\u80cc\u666f\u8272\u3001\u6587\u5b57\u984f\u8272\u7b49\u5c6c\u6027\uff0c\u4f86\u9054\u5230\u81ea\u8a02\u7684\u6548\u679c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\ud83d\udd27Swift-\u5728UITableView\u4e0a\u9762\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0-\u52a0\u5165UIRefreshControl-\ud83d\udd27_1675476875.265059.webp\" alt=\"Swift \u5728 UITableView \u4e0a\u9762\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0 | \u52a0\u5165 UIRefreshControl\" title=\"Swift, UITableView, \u4e0b\u62c9\u5237\u65b0, UIRefreshControl, \u7a0b\u5f0f\u78bc\u7bc4\u4f8b\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728Swift\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528UIRefreshControl\u4f86\u5728UITableView\u4e0a\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0\u529f\u80fd\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u66f4\u65b0\u8cc7\u6599\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728UITableView\u4e0a\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u4f9b\u53c3\u8003\u3002<\/p>\n","protected":false},"author":1,"featured_media":7512,"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-7513","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\/\ud83d\udd27Swift-\u5728UITableView\u4e0a\u9762\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0-\u52a0\u5165UIRefreshControl-\ud83d\udd27_1675476875.265059.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Xb","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7513","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=7513"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7513\/revisions"}],"predecessor-version":[{"id":13413,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7513\/revisions\/13413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7512"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}