두 개의보기 컨트롤러 FirstViewController
및 SecondViewController
이 있습니다. SecondViewController
은 정상적인보기 컨트롤러이며 UITableView
입니다.스위프트 : UITableView 데이터로드 중
FirstViewController
에서 나는 두 번째로 나타납니다. 내가 그것을 호출하면 대리인을 통해 데이터를 전달하려고합니다. 다음 대리인 프로토콜이다
protocol CalculationDelegate{
func calculations(calculation: [Double])
}
를 기본적으로는 두 배의 어레이를 통과한다. FirstViewController
에서 A 버튼을 클릭하면 전환 준비를하고 대표를 설정하기 위해 prepareForSegue
을 사용합니다.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var secondVC = SecondViewController()
secondVC = segue.destinationViewController as SecondViewController
// Set up the delegation, so data is passed to SecondViewController
self.delegate = SecondViewController()
self.delegate?.calculations(wage.allCalculations)
}
이제 SecondViewController
에 나는 내가 단지 UITableView
에로드 할 통과 한 데이터에 액세스 할 수 있습니다. 당신은 내가 대의원 방법에 self.calculations = calculation
를 할당하려고 볼 수 있듯이 여기 SecondViewController
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CalculationDelegate {
@IBOutlet
var tableView: UITableView!
var calculations: [Double] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
// Conform to CalculationsDelegate by implementing this method
func calculations(calculation: [Double]) {
self.calculations = calculation
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.calculations.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = NSString(format: "%.2f", self.calculations[indexPath.row])
return cell
}
}
의 전체 클래스 선언이다. 그런 다음 데이터를 사용하여 UITableView
을로드하면 데이터가로드되지 않습니다. 오류가 체크되어 있고 데이터가 대리자에서 전달되고 있습니다. 어떤 도움이라도 대단히 감사 할 것입니다.
네, 설명이 완벽하게 작동했습니다. –