我试图实现一个UISegmentedControl,以便使用switch语句来决定哪个数组应该填充UIPickerView,但这种用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.
}
}
答案 0 :(得分:2)
您还不了解数据源/代理消息传递的工作原理。此代码不能按照您的想法执行操作:
case 0:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) ->UIView {
这不会神奇地导致此函数成为数据源viewForRow
方法。它只是定义了一个本地函数,它已经存在并立即消失,毫无意义。您必须在视图控制器的顶层实现viewForRow
,就像所有其他方法一样。 它必须根据所选的细分做出选择。 (事实上,我认为你已经设置了它。)分段控件在其值发生变化时,只是告诉选择器视图重新加载(例如reloadAllComponents
),从而导致viewForRow
到再次被召唤。