{"id":7180,"date":"2025-06-03T14:50:59","date_gmt":"2025-06-05T07:12:59","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7180"},"modified":"2025-06-05T14:50:59","modified_gmt":"2025-06-05T07:12:59","slug":"https-badgameshow-com-steven-263","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-263\/","title":{"rendered":"Swift 2025 \u6700\u65b0\u7248 UITableView \u5373\u6642\u641c\u7d22\u529f\u80fd\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UITableView, \u5373\u6642\u641c\u7d22, \u641c\u7d22\u6578\u64da\"><\/p>\n<h1>Swift 2025 \u6700\u65b0\u7248 UITableView \u5373\u6642\u641c\u7d22\u529f\u80fd\u6559\u5b78<\/h1>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c`UITableView` \u662f\u4e00\u500b\u975e\u5e38\u5e38\u898b\u7684\u63a7\u4ef6\uff0c\u5b83\u80fd\u986f\u793a\u4e00\u7cfb\u5217\u7684\u6578\u64da\uff0c\u4e26\u4e14\u53ef\u4ee5\u8f15\u9b06\u642d\u914d\u5176\u4ed6\u63a7\u4ef6\u4ee5\u5be6\u73fe\u66f4\u591a\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 **Swift 2025 \u6700\u65b0\u8a9e\u6cd5** \u4f86\u5be6\u73fe `UITableView` \u7684\u5373\u6642\u641c\u7d22\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u80fd\u65b9\u4fbf\u5730\u5c0d `UITableView` \u4e2d\u7684\u6578\u64da\u9032\u884c\u641c\u7d22\u3002<\/p>\n<h2>\u524d\u7f6e\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u5be6\u73fe `UITableView` \u7684\u5373\u6642\u641c\u7d22\u529f\u80fd\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u5148\u5efa\u7acb\u4e00\u500b `UITableView`\uff0c\u4e26\u5c07\u5b83\u6dfb\u52a0\u5230\u6211\u5011\u7684 `ViewController` \u4e2d\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet tableView = UITableView()<\/p>\n<p>override func viewDidLoad() {<br \/>\n    super.viewDidLoad()<br \/>\n    view.addSubview(tableView)<br \/>\n    setupTableView()<br \/>\n}<\/p>\n<p>private func setupTableView() {<br \/>\n    tableView.dataSource = self<br \/>\n    tableView.delegate = self<br \/>\n    tableView.register(UITableViewCell.self, forCellReuseIdentifier: &#8220;Cell&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u5c07 `UITableView` \u7684 `dataSource` \u548c `delegate` \u8a2d\u7f6e\u70ba\u6211\u5011\u7684 `ViewController`\uff0c\u4e26\u5be6\u73fe `UITableView` \u7684\u5fc5\u8981\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u6211\u5011\u53ef\u4ee5\u5c0d `UITableView` \u4e2d\u7684\u6578\u64da\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<p>&#8220;`swift<br \/>\nfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {<br \/>\n    return filteredData.count<br \/>\n}<\/p>\n<p>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {<br \/>\n    let cell = tableView.dequeueReusableCell(withIdentifier: &#8220;Cell&#8221;, for: indexPath)<br \/>\n    cell.textLabel?.text = filteredData[indexPath.row]\n    return cell<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u5be6\u73fe\u5373\u6642\u641c\u7d22<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u958b\u59cb\u5be6\u73fe `UITableView` \u7684\u5373\u6642\u641c\u7d22\u529f\u80fd\u4e86\u3002\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 `ViewController` \u4e2d\u6dfb\u52a0\u4e00\u500b `UISearchBar`\uff0c\u4e26\u5c07\u5b83\u6dfb\u52a0\u5230 `UITableView` \u7684 `headerView` \u4e2d\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet searchBar = UISearchBar()<br \/>\nvar data: [String] = [&#8220;Apple&#8221;, &#8220;Banana&#8221;, &#8220;Cherry&#8221;, &#8220;Date&#8221;]\nvar filteredData: [String] = []\n<p>override func viewDidLoad() {<br \/>\n    super.viewDidLoad()<br \/>\n    view.addSubview(tableView)<br \/>\n    tableView.tableHeaderView = searchBar<br \/>\n    setupSearchBar()<br \/>\n}<\/p>\n<p>private func setupSearchBar() {<br \/>\n    searchBar.delegate = self<br \/>\n    searchBar.placeholder = &#8220;\u641c\u7d22\u6c34\u679c&#8221;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u5c07 `UISearchBar` \u7684 `delegate` \u8a2d\u7f6e\u70ba\u6211\u5011\u7684 `ViewController`\uff0c\u4e26\u5be6\u73fe `UISearchBar` \u7684\u5fc5\u8981\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u6211\u5011\u53ef\u4ee5\u5728\u7528\u6236\u8f38\u5165\u6587\u5b57\u6642\u5c0d `UITableView` \u4e2d\u7684\u6578\u64da\u9032\u884c\u641c\u7d22\u3002<\/p>\n<p>&#8220;`swift<br \/>\nextension ViewController: UISearchBarDelegate {<br \/>\n    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {<br \/>\n        if searchText.isEmpty {<br \/>\n            filteredData = data<br \/>\n        } else {<br \/>\n            filteredData = data.filter { $0.contains(searchText) }<br \/>\n        }<br \/>\n        tableView.reloadData()<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u5728\u5be6\u73fe\u641c\u7d22\u529f\u80fd\u6642\uff0c\u6211\u5011\u9700\u8981\u5c0d `UITableView` \u4e2d\u7684\u6578\u64da\u9032\u884c\u904e\u6ffe\uff0c\u53ea\u986f\u793a\u7b26\u5408\u7528\u6236\u8f38\u5165\u6587\u5b57\u7684\u6578\u64da\u3002\u7576\u7528\u6236\u6e05\u7a7a\u641c\u7d22\u6846\u6642\uff0c\u6062\u5fa9\u986f\u793a\u6240\u6709\u6578\u64da\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>1. \u78ba\u4fdd\u5728 `viewDidLoad` \u4e2d\u6b63\u78ba\u8a2d\u7f6e `tableView` \u548c `searchBar` \u7684 frame\u3002<br \/>\n2. \u6aa2\u67e5 `UITableViewDataSource` \u548c `UITableViewDelegate` \u662f\u5426\u6b63\u78ba\u9075\u5faa\uff0c\u4e26\u4e14\u8a3b\u518a\u4e86 cell\u3002<br \/>\n3. \u78ba\u4fdd `filteredData` \u7684\u521d\u59cb\u503c\u8207 `data` \u76f8\u540c\uff0c\u4ee5\u514d\u51fa\u73fe\u7a7a\u767d\u5217\u8868\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `UISearchController` \u4f86\u66ff\u63db `UISearchBar`\uff0c\u4ee5\u63d0\u4f9b\u66f4\u70ba\u73fe\u4ee3\u5316\u7684\u641c\u7d22\u9ad4\u9a57\u3002<br \/>\n&#8211; \u63a2\u7d22\u5982\u4f55\u5c07\u641c\u7d22\u529f\u80fd\u8207\u7db2\u7d61\u6578\u64da\u6e90\u6574\u5408\uff0c\u6bd4\u5982\u5f9e API \u7372\u53d6\u6578\u64da\u4e26\u9032\u884c\u5373\u6642\u641c\u7d22\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 **Swift 2025 \u6700\u65b0\u8a9e\u6cd5** \u4f86\u5be6\u73fe `UITableView` \u7684\u5373\u6642\u641c\u7d22\u529f\u80fd\u3002\u5f9e\u5efa\u7acb `UITableView` \u5230\u6dfb\u52a0 `UISearchBar`\uff0c\u518d\u5230\u5be6\u73fe\u641c\u7d22\u529f\u80fd\uff0c\u6211\u5011\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u6b65\u9a5f\u548c\u4ee3\u78bc\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView-\u5373\u6642\u641c\u7d22-\u5373\u6642\u641c\u7d22\u6578\u64da-\ud83d\udd0d_1675404162.150414.webp\" alt=\"Swift UITableView \u5373\u6642\u641c\u7d22 \u5373\u6642\u641c\u7d22\u6578\u64da \ud83d\udd0d\" title=\"Swift UITableView, \u5373\u6642\u641c\u7d22, \u6578\u64da\u641c\u7d22, Swift methods(), UITableView\u641c\u7d22\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 UITableView \u4e2d\u4f7f\u7528\u591a\u500b\u641c\u7d22\u689d\u4ef6\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u64f4\u5c55 `filter` \u65b9\u6cd5\uff0c\u6839\u64da\u591a\u500b\u689d\u4ef6\u9032\u884c\u904e\u6ffe\uff0c\u4f8b\u5982\u6309\u540d\u7a31\u548c\u985e\u5225\u3002<\/p>\n<h3>2. UISearchController \u6709\u4ec0\u9ebc\u512a\u52e2\uff1f<\/h3>\n<p>`UISearchController` \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u7528\u6236\u9ad4\u9a57\uff0c\u652f\u6301\u5728\u7d50\u679c\u4e2d\u986f\u793a\u641c\u7d22\u5efa\u8b70\uff0c\u4e26\u6574\u5408\u4e86\u641c\u7d22\u6642\u7684\u4f48\u5c40\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u641c\u7d22\u6642\u907f\u514d\u986f\u793a\u91cd\u8907\u9805\u76ee\uff1f<\/h3>\n<p>\u5728\u904e\u6ffe\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 `Set` \u4f86\u4fdd\u6301\u552f\u4e00\u6027\uff0c\u6216\u5728 `filter` \u65b9\u6cd5\u4e2d\u52a0\u5165\u689d\u4ef6\u4ee5\u907f\u958b\u91cd\u8907\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528SwiftUITableView\u5be6\u73fe\u5373\u6642\u641c\u7d22\u6578\u64da\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684methods()\u51fd\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u5c0dUITableView\u9032\u884c\u5373\u6642\u641c\u7d22\u3002<\/p>\n","protected":false},"author":1,"featured_media":7179,"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-7180","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-\u5373\u6642\u641c\u7d22-\u5373\u6642\u641c\u7d22\u6578\u64da-\ud83d\udd0d_1675404162.150414.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1RO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7180","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=7180"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7180\/revisions"}],"predecessor-version":[{"id":13172,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7180\/revisions\/13172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7179"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}