为了使用switch语句来决定哪个数组应该填充UIPickerView,我尝试实现UISegmentedControl,但是这种用swift编写的方法并没有达到预期的效果。以前是否有人探讨过这个问题,或者可能对这些功能如何协同工作有任何想法?
以下是目前为止的代码:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var rejuvenatedEnterprisesEmployees = [
Employees(name: "Jeffrey Wells", employeeRoles: ["Accountant", "Designer"], departmentID: 0),
Employees(name: "Victor Atkins", employeeRoles: ["Director of IT", "Sales Stategist"], departmentID: 0),
Employees(name: "Kelli Hamilton", employeeRoles: ["Director of Sales"], departmentID: 0),
Employees(name: "Adam Braun", employeeRoles: ["Sales Lead"], departmentID: 1),
Employees(name: "Brian Cruz", employeeRoles: ["Designer"], departmentID: 1),
Employees(name: "Kristen Floyd", employeeRoles: ["Developer"], departmentID: 1),
Employees(name: "Lois Martinez", employeeRoles: ["Curator", "Blogger"], departmentID: 2),
Employees(name: "Michael Lind", employeeRoles: ["Sales, Administrator"], departmentID: 2),
Employees(name: "Eric Bay", employeeRoles: ["Database Admin"], departmentID: 2),
Employees(name: "Brandon Young", employeeRoles: ["Artist"], departmentID: 2)
]
var rejuvenatedEnterprisesVictorsEmployees = [
Employees(name: "Victor Atkins", employeeRoles: ["Director of IT", "Sales Stategist"], departmentID: 0),
Employees(name: "Adam Braun", employeeRoles: ["Sales Lead"], departmentID: 1),
Employees(name: "Brian Cruz", employeeRoles: ["Designer"], departmentID: 1),
Employees(name: "Kristen Floyd", employeeRoles: ["Developer"], departmentID: 1)
]
var rejuvenatedEnterprisesKellisEmployees = [
Employees(name: "Kelli Hamilton", employeeRoles: ["Director of Sales"], departmentID: 0),
Employees(name: "Lois Martinez", employeeRoles: ["Curator", "Blogger"], departmentID: 2),
Employees(name: "Michael Lind", employeeRoles: ["Sales, Administrator"], departmentID: 2),
Employees(name: "Eric Bay", employeeRoles: ["Database Admin"], departmentID: 2),
Employees(name: "Brandon Young", employeeRoles: ["Artist"], departmentID: 2)
]
var squaredEnterprisesEmployees: [String] = ["Jeffrey Wells","Victor Atkins","Kelli Hamilton","Adam Braun","Brian Cruz","Kristen Floyd","Lois Martinez","Michael Lind","Eric Bay","Brandon Young"]
//UIPicker Scroll component
let kManagerScrollComponentCount: Int = 1
let kManagerScrollComponent: Int = 0
//UIPicker Scroll component
var chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
@IBOutlet weak var employeeListing: UITextView!
@IBOutlet weak var userTicketLocation: UILabel!
@IBAction func addEmployee(sender: AnyObject) {
}
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBAction func indexChanged(sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex
{
case 0:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) ->UIView {
//var chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
chosenManagerScrollView.backgroundColor = UIColor.clearColor()
var jumperCables: [String] = []
for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
jumperCables.append(rejuvenatedEnterprisesEmployees[i].name)
}
chosenManagerScrollView.text = jumperCables[row]
return chosenManagerScrollView
}
case 1:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) ->UIView {
//var chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
chosenManagerScrollView.backgroundColor = UIColor.greenColor()
var jumperCablez: [String] = []
for var i = 0; i < rejuvenatedEnterprisesVictorsEmployees.count ; ++i {
jumperCablez.append(rejuvenatedEnterprisesVictorsEmployees[i].name)
}
chosenManagerScrollView.text = jumperCablez[row]
return chosenManagerScrollView
}
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return kManagerScrollComponentCount
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return rejuvenatedEnterprisesEmployees.count
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) ->UIView {
switch segmentedControl.selectedSegmentIndex
{
case 0:
let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
chosenManagerScrollView.backgroundColor = UIColor.clearColor()
var jumperCables: [String] = []
for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
jumperCables.append(rejuvenatedEnterprisesEmployees[i].name)
}
chosenManagerScrollView.text = jumperCables[row]
return chosenManagerScrollView
case 1:
let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
chosenManagerScrollView.backgroundColor = UIColor.greenColor()
var jumperCablez: [String] = []
for var i = 0; i < rejuvenatedEnterprisesVictorsEmployees.count ; ++i {
jumperCablez.append(rejuvenatedEnterprisesVictorsEmployees[i].name)
}
chosenManagerScrollView.text = jumperCablez[row]
return chosenManagerScrollView
default:
print("done")
let chosenManagerScrollView: UILabel = UILabel(frame: CGRectMake(40,0,170,72))
chosenManagerScrollView.backgroundColor = UIColor.clearColor()
var jumperCables: [String] = []
for var i = 0; i < rejuvenatedEnterprisesEmployees.count ; ++i {
jumperCables.append(rejuvenatedEnterprisesEmployees[i].name)
}
chosenManagerScrollView.text = jumperCables[row]
return chosenManagerScrollView
}
}
func displayEmployeeGroup(chosenEmployee: String, fromComponent chosenComponent: String) {
switch chosenEmployee {
case chosenEmployee where chosenEmployee == "Jeffrey Wells":
for employee in rejuvenatedEnterprisesEmployees {
print(employee)
employeeListing.text = "\(employee)\n"
}
case chosenEmployee where chosenEmployee == "Victor Atkins":
break
case chosenEmployee where chosenEmployee == "Kelli Hamilton":
break
default: print("done")
}
if chosenEmployee == "Jeffrey Wells" {
for employee in rejuvenatedEnterprisesEmployees {
print(employee)
employeeListing.text = "\(employee)\n"
}
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let chosenEmployee: Int = pickerView.selectedRowInComponent(kManagerScrollComponent)
displayEmployeeGroup(squaredEnterprisesEmployees[chosenEmployee], fromComponent: "the list of employees")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
您还不了解数据源/委托消息传递的工作原理。这段代码并不像你想象的那样:
case 0:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) ->UIView {
这不会神奇地使此函数成为数据源
viewForRow
方法。它只是定义了一个局部函数,它即刻出现并消失,没有任何意义。您必须像所有其他方法一样在视图控制器的顶层实现viewForRow
。它必须根据选定的段进行选择。(事实上,我认为您已经设置好了。)分段控件在其值更改时,只会告诉picker视图重新加载(例如reloadAllComponents
),从而导致再次调用viewForRow
。