2016-10-18 4 views
2

設定画面を作成しようとしています。私は空のビューコントローラにテーブルビューを追加することができましたが、セルの数に応じてテーブルビューを拡大したり折りたたんだりするなど、いくつかの文体的​​な制限がありました。テーブルビューのセルでステッパーを使用していますが、VCに返すデータが正しく更新されていません。

私はtableviewcontrollerで画面を設定すると考えました。 ステッパーsection 0cell 1に入れて、section 1の行数を増減させたいところまで、今はうまくいっています。これは、データ配列(workouts[])が最新の値(cellForRowAt:indexPathメソッド)でビューコントローラを更新し、numberOfRowsInSectionを(おそらく)最新のworkouts.countで更新する程度まで機能します。

しかし、workouts.countは、アレイが最後にステッパーによって更新される前にカウントが保持されていることが分かります。

今、私はいくつかの方法を試しましたが、まだnumberOfRowsInSectionメソッドを正しく更新することができません。ワークアウト配列はView Controllerで最新の値で更新されていないようです前の設定値。

これは私のビューコントローラである:

class AViewController: UITableViewController { 

//MARK: Properties 
@IBOutlet weak var aTableView: UITableView! 

var sections: [String] = ["", "Set Up Circuits"] 
var workouts: [Exercise] = [] 

//MARK: Initialisation 
override func viewDidLoad() { 
    super.viewDidLoad() 
    workouts = [Exercise(name: "Circuit 1", timeMinutes: 2)] 
    self.tableView.delegate = self 
    self.tableView.dataSource = self 
} 

//MARK: Table Config 
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return self.sections[section] 
} 

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    switch section { 
    case 0: 
     return 0.1 
    default: 
     return 32.0 
    } 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    switch indexPath.section { 
    case 0: 
     return 60 
    default: 
     return 44 
    } 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    var numberOfRowsInSection: Int = 0 

    switch section { 
    case 0: 
     numberOfRowsInSection = 1 
    default: 
     numberOfRowsInSection = workouts.count 
     //numberOfRowsInSection = 1 
    } 
    print(numberOfRowsInSection, "number of rows in section", section) 
    return numberOfRowsInSection 
} 

//MARK: Table Protocols 
override func numberOfSections(in tableView: UITableView) -> Int { 
    return sections.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    switch indexPath.section { 

    //Setting layout for circuit number config 
    case 0: 
     let cell = aTableView.dequeueReusableCell(withIdentifier: "ACircuitTableViewCell") as! ACircuitTableViewCell 
     cell.tableView = self.aTableView 
     self.workouts = cell.workouts 
     print(workouts.count, " VC workouts array count") 
     return cell 

    //Setting layout for circuit cells 
    default: 
     let cell = aTableView.dequeueReusableCell(withIdentifier: "ATableViewCell") as! ATableViewCell 
     cell.aLabel.text = workouts[indexPath.row].getName() 
     cell.aDetail.text = "Tap here to configure" 
     return cell 

    } 
} 

これは私のACircuitTableViewCellです:

class ACircuitTableViewCell: UITableViewCell { 

//MARK: Properties 
@IBOutlet weak var circuitNumber: UILabel! 
@IBOutlet weak var circuitLabel: UILabel! 
@IBOutlet weak var circStepper: UIStepper! 


var tableView: UITableView! 
// var updateCallback : ((_ updateList: Bool)-> Void)? 

//var exercises: [Exercise]? 
var anArray: [Exercise] = [] 
var workouts: [Exercise] = [Exercise(name: "Circuit 1", timeMinutes: 2)] 

var workoutsCount: Int = 1 
var indexPath: NSIndexPath = NSIndexPath(row: 0, section: 1) 
//MARK: Original Functions 
override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 

    circuitNumber.text = String(workouts.count) 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

@IBAction func valueChanged(_ sender: AnyObject) { 
    print("Method .valueChanged activated") 

    //Update circuitNumber label 
    circuitNumber.text = String(Int(circStepper.value)) 

    //View pre-array-update status of variables 
    print(workouts.count, "anArray before appending") 
    print(circStepper.value, "stepper value before appending") 
    print(circuitNumber.text, "circuitNumber") 

    //Update workouts array 
    if workouts.count < Int(circStepper.value) { 
     workouts.append(Exercise(name: "Circuit \(Int(circStepper.value))", timeMinutes: 2)) 
     print(workouts.count, "after appending") 

     tableView.reloadData() 

     print(workouts.count, "new workout.count") 
    } 


    print(workouts.count, "workouts.count") 
    print("Method .valueChanged completed") 

} 

これは、ステッパ+ボタン上の3つのタップのときのアプリのロードからコンソールに出力されます。

1 number of rows in section 1 
1 number of rows in section 0 
1 number of rows in section 1 
1 number of rows in section 0 
1 number of rows in section 1 
1 number of rows in section 0 
1 VC workouts array count 
Method .valueChanged activated 
1 anArray before appending 
2.0 stepper value before appending 
Optional("2") circuitNumber 
2 after appending 
1 number of rows in section 1 
1 number of rows in section 0 
2 new workout.count 
2 workouts.count 
Method .valueChanged completed 
2 VC workouts array count 
Method .valueChanged activated 
2 anArray before appending 
3.0 stepper value before appending 
Optional("3") circuitNumber 
3 after appending 
2 number of rows in section 1 
1 number of rows in section 0 
3 new workout.count 
3 workouts.count 
Method .valueChanged completed 
3 VC workouts array count 
Method .valueChanged activated 
3 anArray before appending 
4.0 stepper value before appending 
Optional("4") circuitNumber 
4 after appending 
3 number of rows in section 1 
1 number of rows in section 0 
4 new workout.count 
4 workouts.count 
Method .valueChanged completed 
4 VC workouts array count 
Method .valueChanged activated 
4 anArray before appending 
5.0 stepper value before appending 
Optional("5") circuitNumber 
5 after appending 
4 number of rows in section 1 
1 number of rows in section 0 
5 new workout.count 
5 workouts.count 
Method .valueChanged completed 
5 VC workouts array count 
+0

セル(または他のビュー)にデータソースを格納しないでください。 MVCパターンを破ります。あなたがするべきことは、コントローラ内のステッパーコールバックを処理し、そこでviewControllerのdataSourceを変更することです。 – alexburtnik

+0

@alexburtnikありがとう!いったん私はコントローラのステッパーコールバックを処理する方法を考え出すと、テーブルは私が望むように機能しました。 – Sonera

+0

ようこそ。 viewControllerでビューイベントを処理するための2つの一般的なアプローチを投稿しました。あなたはおそらくそれらの1つを使用しました – alexburtnik

答えて

0

コントローラでステッパーコールバックを処理し、そこでviewControllerのdataSourceを変更する必要があります。

  1. 私がここで述べてきたように、ビューコントローラ内のセルのイベントを処理するための最も一般的な方法は、デリゲートを使用することです: https://stackoverflow.com/a/40111943/1689376

  2. 代わりにあなたが正しいcellForRow方法でselfターゲットとアクションを追加することができます:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell 
    cell.stepper?.tag = indexPath.row 
    cell.stepper?.addTarget(self, action: #selector(stepperValueChanged(_:)), for: .valueChanged) 
    return cell 
} 

func stepperValueChanged(_ stepper: UIStepper) { 
    //use stepper.tag as index to fetch an object from your dataSource 
    let workout = dataSource[stepper.tag] 
} 
関連する問題