{"id":4011,"date":"2025-05-31T11:16:09","date_gmt":"2025-06-05T03:57:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4011"},"modified":"2025-06-05T11:16:09","modified_gmt":"2025-06-05T03:57:09","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e9%80%89%e6%8b%a9%e5%99%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e9%80%89%e6%8b%a9%e5%99%a8\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u5982\u4f55\u4f7f\u7528 UIPickerView \u5be6\u73fe\u9078\u64c7\u5668"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"Swift, UIPickerView, \u9009\u62e9\u5668, \u6570\u636e\u6e90, \u4ee3\u7406, \u5916\u89c2, iOS \u5f00\u53d1, \u7f16\u7a0b\u6559\u7a0b\"><\/p>\n<h1>2025 \u6700\u65b0\u7248 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u5982\u4f55\u4f7f\u7528 UIPickerView \u5be6\u73fe\u9078\u64c7\u5668<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u9069\u7528\u65bc\u5275\u5efa\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\uff0c\u5c24\u5176\u662f iOS \u61c9\u7528\u7a0b\u5f0f\u3002\u5728 iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u9078\u64c7\u5668\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u7528\u6236\u754c\u9762\u5143\u7d20\uff0c\u5e6b\u52a9\u7528\u6236\u5f9e\u4e00\u7d44\u9078\u9805\u4e2d\u9032\u884c\u9078\u64c7\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4e2d\u7684 UIPickerView \u985e\u4f86\u5be6\u73fe\u9078\u64c7\u5668\uff0c\u6db5\u84cb\u6578\u64da\u6e90\u548c\u4ee3\u7406\u8a2d\u7f6e\uff0c\u4ee5\u53ca\u5982\u4f55\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u5916\u89c0\u548c\u884c\u70ba\u3002<\/p>\n<h2>\u4f7f\u7528 UIPickerView \u985e\u5be6\u73fe\u9078\u64c7\u5668<\/h2>\n<p>UIPickerView \u985e\u662f iOS \u4e2d\u7528\u65bc\u5be6\u73fe\u9078\u64c7\u5668\u7684\u985e\uff0c\u986f\u793a\u4e00\u7d44\u53ef\u4f9b\u7528\u6236\u9078\u64c7\u7684\u9078\u9805\u3002\u9996\u5148\u9700\u8981\u5728\u754c\u9762\u4e2d\u6dfb\u52a0\u4e00\u500b UIPickerView \u5c0d\u8c61\uff0c\u7136\u5f8c\u8a2d\u7f6e\u5b83\u7684\u6578\u64da\u6e90\u548c\u4ee3\u7406\u3002<\/p>\n<h2>\u8a2d\u7f6e\u9078\u64c7\u5668\u7684\u6578\u64da\u6e90\u548c\u4ee3\u7406<\/h2>\n<p>\u8981\u8a2d\u7f6e UIPickerView \u7684\u6578\u64da\u6e90\u548c\u4ee3\u7406\uff0c\u9700\u8981\u5be6\u73fe <code>UIPickerViewDataSource<\/code> \u548c <code>UIPickerViewDelegate<\/code> \u5354\u8b70\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u8a2d\u7f6e\u6578\u64da\u6e90\u7684\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: swift\">\n\/\/ \u8a2d\u7f6e\u6578\u64da\u6e90\nfunc numberOfComponents(in pickerView: UIPickerView) -> Int {\n    return 3 \/\/ \u8fd4\u56de\u9078\u64c7\u5668\u7684\u5217\u6578\n}\n\nfunc pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {\n    return 10 \/\/ \u8fd4\u56de\u6bcf\u5217\u7684\u884c\u6578\n}\n\nfunc pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {\n    return \"\u9078\u9805 \\(row)\" \/\/ \u8fd4\u56de\u6bcf\u884c\u7684\u6a19\u984c\n}\n<\/pre>\n<p>\u4ee5\u4e0b\u662f\u8a2d\u7f6e\u4ee3\u7406\u7684\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: swift\">\n\/\/ \u8a2d\u7f6e\u4ee3\u7406\nfunc pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {\n    print(\"\u5df2\u9078\u64c7\u7b2c \\(row) \u884c\u65bc\u7b2c \\(component) \u5217\")\n}\n\nfunc pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {\n    let label = UILabel()\n    label.text = \"\u9078\u9805 \\(row)\"\n    label.textAlignment = .center\n    label.textColor = UIColor.red\n    return label\n}\n<\/pre>\n<h2>\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u5916\u89c0<\/h2>\n<p>UIPickerView \u63d0\u4f9b\u4e86\u4e00\u7d44\u65b9\u6cd5\uff0c\u53ef\u7528\u65bc\u81ea\u5b9a\u7fa9\u9078\u64c7\u5668\u7684\u5916\u89c0\uff1a<\/p>\n<pre class=\"brush: swift\">\n\/\/ \u81ea\u5b9a\u7fa9\u5916\u89c0\nfunc pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {\n    return 44.0 \/\/ \u8a2d\u7f6e\u6bcf\u884c\u7684\u9ad8\u5ea6\n}\n\nfunc pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {\n    return 100.0 \/\/ \u8a2d\u7f6e\u6bcf\u5217\u7684\u5bec\u5ea6\n}\n<\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 UIPickerView \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u672a\u6b63\u78ba\u8a2d\u7f6e\u6578\u64da\u6e90\u548c\u4ee3\u7406\u3002\u8acb\u78ba\u4fdd\u60a8\u7684 UIViewController \u7e7c\u627f\u81ea <code>UIPickerViewDataSource<\/code> \u548c <code>UIPickerViewDelegate<\/code>\uff0c\u4e26\u5728 <code>viewDidLoad<\/code> \u65b9\u6cd5\u4e2d\u8a2d\u7f6e\uff1a<\/p>\n<pre class=\"brush: swift\">\noverride func viewDidLoad() {\n    super.viewDidLoad()\n    myPickerView.dataSource = self\n    myPickerView.delegate = self\n}\n<\/pre>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u5c07 UIPickerView \u8207\u5176\u4ed6 UI \u5143\u7d20\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982 UISwitch \u6216 UIButton\uff0c\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u7528\u6236\u4ea4\u4e92\u3002\u4e5f\u53ef\u4ee5\u8003\u616e\u5c07\u9078\u64c7\u5668\u7684\u9078\u9805\u52d5\u614b\u751f\u6210\uff0c\u4f8b\u5982\u5f9e API \u7372\u53d6\u6578\u64da\u4e26\u66f4\u65b0 UIPickerView \u7684\u5167\u5bb9\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4e2d\u7684 UIPickerView \u5be6\u73fe\u9078\u64c7\u5668\uff0c\u5305\u62ec\u8a2d\u7f6e\u6578\u64da\u6e90\u3001\u4ee3\u7406\u4ee5\u53ca\u81ea\u5b9a\u7fa9\u5916\u89c0\u3002\u900f\u904e UIPickerView \u985e\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u6253\u9020\u529f\u80fd\u5f37\u5927\u7684\u9078\u64c7\u5668\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 UIPickerView \u4e2d\u986f\u793a\u5716\u7247\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u5728 <code>viewForRow<\/code> \u65b9\u6cd5\u4e2d\u8fd4\u56de\u4e00\u500b\u5305\u542b UIImageView \u7684\u81ea\u5b9a\u7fa9\u8996\u5716\uff0c\u4ee5\u986f\u793a\u5716\u7247\u3002<\/p>\n<h3>Q2: UIPickerView \u652f\u6301\u591a\u7a2e\u9078\u64c7\u6a21\u5f0f\u55ce\uff1f<\/h3>\n<p>A2: UIPickerView \u672c\u8eab\u4e0d\u652f\u6301\u591a\u9078\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u500b UIPickerView \u4f86\u5be6\u73fe\u985e\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u8655\u7406\u9078\u64c7\u5668\u7684\u6578\u64da\u66f4\u65b0\uff1f<\/h3>\n<p>A3: \u60a8\u53ef\u4ee5\u5728\u6578\u64da\u6e90\u4e2d\u52d5\u614b\u66f4\u65b0\u9078\u64c7\u9805\uff0c\u4e26\u8abf\u7528 <code>reloadAllComponents<\/code> \u65b9\u6cd5\u4f86\u5237\u65b0 UIPickerView \u7684\u986f\u793a\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\u4e2d\u7684\u9009\u62e9\u5668\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528UIPickerView\u7c7b\u6765\u5b9e\u73b0\u9009\u62e9\u5668\u3001\u5982\u4f55\u8bbe\u7f6e\u9009\u62e9\u5668\u7684\u6570\u636e\u6e90\u548c\u4ee3\u7406\u3001\u5982\u4f55\u81ea\u5b9a\u4e49\u9009\u62e9\u5668\u7684\u5916\u89c2\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","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-4011","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-12H","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4011","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=4011"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4011\/revisions"}],"predecessor-version":[{"id":4012,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4011\/revisions\/4012"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}