{"id":7160,"date":"2025-05-31T14:21:54","date_gmt":"2025-06-05T07:20:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7160"},"modified":"2025-06-05T14:21:54","modified_gmt":"2025-06-05T07:20:53","slug":"https-badgameshow-com-steven-253","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-253\/","title":{"rendered":"Swift \u8868\u683c\u5f0f\u756b\u9762 (UITableView) &#8211; 2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udcca"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITableView, iOS \u958b\u767c, UITableView \u6559\u5b78\"><\/p>\n<h1>Swift \u8868\u683c\u5f0f\u756b\u9762 (UITableView) &#8211; 2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udcca<\/h1>\n<p>Swift \u8868\u683c\u5f0f\u756b\u9762 (UITableView) \u662f iOS \u958b\u767c\u4e2d\u6700\u5e38\u898b\u7684\u756b\u9762\u4e4b\u4e00\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u5efa\u7acb\u8868\u683c\u5f0f\u7684\u754c\u9762\uff0c\u8b93\u4f7f\u7528\u8005\u66f4\u8f15\u9b06\u5730\u95b1\u8b80\u8cc7\u6599\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5efa\u7acb UITableView\uff0c\u4e26\u63d0\u4f9b\u5168\u9762\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u53ca\u5be6\u4f5c\u7d30\u7bc0\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2>\u5efa\u7acb UITableView<\/h2>\n<p>\u8981\u5efa\u7acb UITableView\uff0c\u9996\u5148\u9700\u8981\u5728 Storyboard \u4e2d\u65b0\u589e\u4e00\u500b UITableView\uff0c\u4e26\u5c07\u5176\u62d6\u66f3\u81f3 ViewController \u4e2d\u3002\u63a5\u8457\uff0c\u5c07 UITableView \u7684 delegate \u548c dataSource \u9023\u63a5\u5230 ViewController\uff0c\u5177\u9ad4\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/X3U6fhU.png\" alt=\"UITableView \u9023\u63a5\u793a\u610f\u5716\" title=\"\"><\/p>\n<h3>\u8a2d\u5b9a ViewController<\/h3>\n<p>\u5728 ViewController \u4e2d\uff0c\u6211\u5011\u9700\u8981\u5be6\u4f5c UITableViewDelegate \u548c UITableViewDataSource \u65b9\u6cd5\uff0c\u4ee5\u8b93 UITableView \u6b63\u78ba\u986f\u793a\u8cc7\u6599\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {\n    @IBOutlet weak var tableView: UITableView!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        tableView.delegate = self\n        tableView.dataSource = self\n    }\n\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n        \/\/ \u8fd4\u56de\u8981\u986f\u793a\u7684\u884c\u6578\n        return data.count \/\/ \u5047\u8a2d\u6709\u4e00\u500b\u8cc7\u6599\u9663\u5217\n    }\n\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n        \/\/ \u8fd4\u56de\u8981\u986f\u793a\u7684 cell\n        let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n        cell.textLabel?.text = data[indexPath.row] \/\/ \u5047\u8a2d\u8cc7\u6599\u662f\u5b57\u4e32\n        return cell\n    }\n}\n<\/code><\/pre>\n<h2>\u8a2d\u5b9a UITableViewCell<\/h2>\n<p>\u5728 <code>cellForRowAt<\/code> \u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>dequeueReusableCell<\/code> \u65b9\u6cd5\u4f86\u53d6\u5f97\u53ef\u91cd\u8907\u4f7f\u7528\u7684 cell\uff0c\u4e26\u8a2d\u5b9a\u5176\u5167\u5bb9\u3002\u8acb\u53c3\u8003\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n    let cell = tableView.dequeueReusableCell(withIdentifier: \"Cell\", for: indexPath)\n    \/\/ \u8a2d\u5b9a cell \u7684\u5167\u5bb9\n    cell.textLabel?.text = \"\u884c\u6578\uff1a\\(indexPath.row + 1)\" \/\/ \u7bc4\u4f8b\u5167\u5bb9\n    return cell\n}\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5728\u4f7f\u7528 UITableView \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u672a\u9023\u63a5 delegate \u548c dataSource\uff1a<\/strong>\u78ba\u4fdd\u5728 Storyboard \u4e2d\u6b63\u78ba\u9023\u63a5\u3002<\/li>\n<li><strong>cell \u6a19\u8b58\u7b26\u932f\u8aa4\uff1a<\/strong>\u6aa2\u67e5\u5728 Storyboard \u4e2d\u8a2d\u5b9a\u7684\u6a19\u8b58\u7b26\u662f\u5426\u8207\u7a0b\u5f0f\u78bc\u4e2d\u4e00\u81f4\u3002<\/li>\n<li><strong>\u8fd4\u56de\u884c\u6578\u70ba\u96f6\uff1a<\/strong>\u78ba\u4fdd <code>numberOfRowsInSection<\/code> \u65b9\u6cd5\u8fd4\u56de\u6b63\u78ba\u7684\u884c\u6578\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684 UITableView \u61c9\u7528\uff0c\u60a8\u9084\u53ef\u4ee5\u64f4\u5c55\u5982\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\u591a\u500b section \u7684 UITableView\u3002<\/li>\n<li>\u81ea\u5b9a\u7fa9 UITableViewCell \u7684\u6a23\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528 SwiftUI \u4f86\u5efa\u7acb\u8868\u683c\u5f0f\u756b\u9762\u3002<\/li>\n<\/ul>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5efa\u7acb UITableView\uff0c\u4e26\u63d0\u4f9b\u4e86\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\u53ca\u5be6\u4f5c\u6b65\u9a5f\u3002\u7121\u8ad6\u60a8\u662f\u65b0\u624b\u9084\u662f\u7d93\u9a57\u8c50\u5bcc\u7684\u958b\u767c\u8005\uff0c\u9019\u4e9b\u77e5\u8b58\u90fd\u80fd\u5e6b\u52a9\u60a8\u5feb\u901f\u958b\u767c\u51fa\u8868\u683c\u5f0f\u7684\u756b\u9762\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1\uff1a\u5982\u4f55\u5728 UITableView \u4e2d\u6dfb\u52a0\u5716\u7247\uff1f<\/h3>\n<p>A1\uff1a\u60a8\u53ef\u4ee5\u5728 <code>cellForRowAt<\/code> \u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528 <code>cell.imageView?.image = UIImage(named: \"imageName\")<\/code> \u4f86\u8a2d\u5b9a\u5716\u7247\u3002<\/p>\n<h3>Q2\uff1aUITableView \u548c UICollectionView \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>A2\uff1aUITableView \u662f\u7528\u65bc\u986f\u793a\u55ae\u4e00\u5217\u7684\u8cc7\u6599\uff0c\u800c UICollectionView \u53ef\u4ee5\u7528\u65bc\u986f\u793a\u591a\u7a2e\u6392\u5217\u65b9\u5f0f\u7684\u8cc7\u6599\uff0c\u5982\u7db2\u683c\u3002<\/p>\n<h3>Q3\uff1a\u5982\u4f55\u5be6\u73fe UITableView \u7684\u522a\u9664\u529f\u80fd\uff1f<\/h3>\n<p>A3\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>tableView.deleteRows(at:with:)<\/code> \u65b9\u6cd5\u4f86\u522a\u9664\u6307\u5b9a\u7684\u884c\uff0c\u4e26\u5728\u8cc7\u6599\u6e90\u4e2d\u66f4\u65b0\u76f8\u61c9\u7684\u8cc7\u6599\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8868\u683c\u5f0f\u756b\u9762-UITableView-\ud83d\udcca_1675403708.485847.webp\" alt=\"Swift \u8868\u683c\u5f0f\u756b\u9762 (UITableView) \ud83d\udcca\" title=\"Swift \u8868\u683c\u5f0f\u756b\u9762, UITableView, Swift, \u8868\u683c\u5f0f\u756b\u9762, \u8868\u683c\u5f0f\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728Swift\u4e2d\uff0cUITableView\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u5275\u5efa\u8868\u683c\u5f0f\u756b\u9762\uff0c\u4e26\u63d0\u4f9b\u8c50\u5bcc\u7684\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5275\u5efa\u51fa\u7b26\u5408\u9700\u6c42\u7684\u8868\u683c\u5f0f\u756b\u9762\u3002<\/p>\n","protected":false},"author":1,"featured_media":7159,"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-7160","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-\u8868\u683c\u5f0f\u756b\u9762-UITableView-\ud83d\udcca_1675403708.485847.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ru","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7160","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=7160"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7160\/revisions"}],"predecessor-version":[{"id":13187,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7160\/revisions\/13187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7159"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}