{"id":6739,"date":"2025-06-03T12:43:53","date_gmt":"2025-06-05T05:10:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6739"},"modified":"2025-06-05T12:43:53","modified_gmt":"2025-06-05T05:10:53","slug":"https-badgameshow-com-steven-46","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-46\/","title":{"rendered":"Swift UITableView \u591a\u9078\u529f\u80fd\uff1a\u5b8c\u6574\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UITableViewDelegate, \u591a\u9078, iOS, UITableView, Swift \u591a\u9078\u529f\u80fd\"><\/p>\n<h1>Swift UITableView \u591a\u9078\u529f\u80fd\uff1a\u5b8c\u6574\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09<\/h1>\n<p>\u5728\u958b\u767c iOS \u61c9\u7528\u6642\uff0c\u4f7f\u7528 UITableView \u4f86\u5efa\u7acb\u53ef\u591a\u9078\u7684\u5217\u8868\u662f\u4e00\u500b\u5e38\u898b\u9700\u6c42\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6559\u4f60\u5982\u4f55\u4f7f\u7528 UITableViewDelegate \u5be6\u73fe\u591a\u9078\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5be6\u4f5c\u6b65\u9a5f<\/h2>\n<p>\u8981\u5be6\u73fe UITableView \u7684\u591a\u9078\u529f\u80fd\uff0c\u9996\u5148\u9700\u8981\u8a2d\u7f6e UITableView \u7684\u5c6c\u6027\uff0c\u7136\u5f8c\u5be6\u4f5c\u76f8\u95dc\u7684 delegate \u65b9\u6cd5\u3002<\/p>\n<h3>1. \u8a2d\u7f6e UITableView \u7684\u5c6c\u6027<\/h3>\n<p>\u5728 UITableView \u7684\u521d\u59cb\u5316\u904e\u7a0b\u4e2d\uff0c\u8a2d\u5b9a\u5176\u5141\u8a31\u591a\u9078\uff1a<br \/>\n&#8220;`swift<br \/>\ntableView.allowsMultipleSelection = true<br \/>\n&#8220;`<\/p>\n<h3>2. \u5be6\u4f5c UITableViewDelegate \u65b9\u6cd5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5be6\u4f5c `didSelectRowAt` \u65b9\u6cd5\u4f86\u8655\u7406\u9078\u64c7\u884c\u7684\u60c5\u6cc1\uff1a<br \/>\n&#8220;`swift<br \/>\nfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {<br \/>\n    if let cell = tableView.cellForRow(at: indexPath) {<br \/>\n        cell.accessoryType = (cell.accessoryType == .checkmark) ? .none : .checkmark<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<br \/>\n\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5728\u4f7f\u7528\u8005\u9ede\u64ca UITableView \u7684\u6bcf\u4e00\u500b cell \u6642\uff0c\u5207\u63db\u5176 accessoryType\u3002<\/p>\n<h3>3. \u8a2d\u7f6e cell \u7684\u986f\u793a\u72c0\u614b<\/h3>\n<p>\u5728 `cellForRowAt` \u65b9\u6cd5\u4e2d\uff0c\u6839\u64da\u9078\u64c7\u72c0\u614b\u4f86\u8a2d\u7f6e cell \u7684 accessoryType\uff1a<br \/>\n&#8220;`swift<br \/>\nfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {<br \/>\n    let cell = tableView.dequeueReusableCell(withIdentifier: &#8220;Cell&#8221;, for: indexPath)<br \/>\n    cell.accessoryType = tableView.indexPathForSelectedRow == indexPath ? .checkmark : .none<br \/>\n    return cell<br \/>\n}<br \/>\n&#8220;`<br \/>\n\u9019\u6a23\u5c31\u80fd\u5920\u5728 UITableView \u4e2d\u5be6\u73fe\u591a\u9078\u7684\u529f\u80fd\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u9078\u64c7\u591a\u500b\u9805\u76ee\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>&#8211; **\u7121\u6cd5\u9078\u64c7\u591a\u500b\u9805\u76ee**\uff1a\u78ba\u4fdd `allowsMultipleSelection` \u8a2d\u5b9a\u70ba `true`\u3002<br \/>\n&#8211; **\u9078\u64c7\u72c0\u614b\u4e0d\u66f4\u65b0**\uff1a\u6aa2\u67e5 `cellForRowAt` \u65b9\u6cd5\u4e2d\u7684\u908f\u8f2f\uff0c\u78ba\u4fdd\u6839\u64da\u9078\u64c7\u72c0\u614b\u6b63\u78ba\u8a2d\u7f6e accessoryType\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u4f60\u53ef\u4ee5\u5c07\u591a\u9078\u7d50\u679c\u5132\u5b58\u5230\u9663\u5217\u4e2d\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u64cd\u4f5c\uff0c\u4f8b\u5982\u63d0\u4ea4\u9078\u64c7\u7684\u9805\u76ee\u3002<br \/>\n&#8211; \u6574\u5408\u8207\u5176\u4ed6 UIKit \u5143\u4ef6\uff0c\u5982 UIButton \u6216 UIAlertController\uff0c\u4f86\u9032\u4e00\u6b65\u589e\u5f37\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView\u591a\u9078\uff1aUITableViewDelegate-\ud83d\udce9_1675394875.336392.webp\" alt=\"Swift UITableView\u591a\u9078\uff1aUITableViewDelegate \ud83d\udce9\" title=\"Swift UITableViewDelegate, UITableView\u591a\u9078, iOS\u591a\u9078\u529f\u80fd, Swift\u591a\u9078\u529f\u80fd, iOS\u61c9\u7528\u7a0b\u5e8f\u591a\u9078\" 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\u53d6\u6d88\u9078\u64c7\u7684\u9805\u76ee\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u5728 `didDeselectRowAt` \u65b9\u6cd5\u4e2d\u5c07 cell \u7684 accessoryType \u8a2d\u70ba `.none`\uff0c\u4f86\u53d6\u6d88\u9078\u64c7\u7684\u9805\u76ee\u3002<\/p>\n<h3>2. UITableView \u7684\u591a\u9078\u529f\u80fd\u6709\u4f55\u9650\u5236\uff1f<\/h3>\n<p>\u4e3b\u8981\u9650\u5236\u662f\u60a8\u5fc5\u9808\u624b\u52d5\u7ba1\u7406\u9078\u64c7\u72c0\u614b\uff0c\u4e26\u78ba\u4fdd\u9069\u7576\u66f4\u65b0 cell \u7684\u986f\u793a\u72c0\u614b\u3002<\/p>\n<h3>3. \u5982\u4f55\u5f9e UITableView \u4e2d\u7372\u53d6\u6240\u6709\u9078\u64c7\u7684\u9805\u76ee\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 `indexPathsForSelectedRows` \u4f86\u7372\u53d6\u6240\u6709\u9078\u64c7\u7684\u884c\uff0c\u7136\u5f8c\u900f\u904e\u9019\u4e9b indexPaths \u4f86\u7372\u53d6\u5c0d\u61c9\u7684\u8cc7\u6599\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u512a\u5316\u4e0d\u50c5\u66f4\u65b0\u4e86\u5167\u5bb9\uff0c\u9084\u589e\u5f37\u4e86 SEO \u6548\u679c\uff0c\u4e26\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u6559\u5b78\u548c\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff0c\u4ee5\u63d0\u5347\u8b80\u8005\u7684\u5b78\u7fd2\u9ad4\u9a57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftUITableViewDelegate\u4f86\u5be6\u73feUITableView\u591a\u9078\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728iOS\u61c9\u7528\u7a0b\u5e8f\u4e2d\u5be6\u73fe\u591a\u9078\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":6738,"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-6739","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\u591a\u9078\uff1aUITableViewDelegate-\ud83d\udce9_1675394875.336392.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1KH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6739","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=6739"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6739\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6738"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}