사용자가 원하는 운동 수를 설정할 수 있도록 구성 화면을 만들려고합니다. 저는 빈 뷰 컨트롤러에 tableview를 추가 할 수있었습니다 만, 셀의 수에 따라 테이블 뷰를 확장하거나 축소하는 것과 같은 몇 가지 문체 제한이있었습니다.표보기 셀에서 스테퍼 사용 - VC로 돌아 오는 데이터가 올바르게 업데이트되지 않음
그래서 나는 tableviewcontroller
에 화면을 설정할 것이라고 생각했습니다. 이제는 내 스테퍼의 section 0
, cell 1
에 section 1
의 행 수를 늘리거나 줄이기를 원할 때까지는 모든 것이 잘 진행됩니다. 데이터 배열 (workouts[]
)이보기 컨트롤러를 최신 값 (cellForRowAt:indexPath
메서드)으로 업데이트하고 numberOfRowsInSection을 (아마도) 최신 workouts.count
으로 업데이트하는 정도까지 작동합니다.
그러나 스테퍼가 마지막으로 어레이를 업데이트하기 전에 workouts.count
이 카운트를 유지합니다.
이제 여러 가지 해결 방법을 시도했지만 올바르게 업데이트하려면 numberOfRowsInSection
메서드를 가져올 수 없습니다. 워크 아웃 배열이 View Controller에서 최신 값으로 업데이트되지 않은 것 같습니다. 이전 설정 값. 이것은 내 ACircuitTableViewCell입니다
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
}
}
:
이
내보기 컨트롤러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")
}
이 스테퍼 + 버튼을 세 가지 탭에 때 응용 프로그램로드에서 콘솔에 출력한다 .
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
셀 (또는 다른 뷰)에 데이터 소스를 저장하지 마십시오. MVC 패턴이 손상됩니다. 컨트롤러에서 스테퍼 콜백을 처리하고 viewController의 dataSource를 변경해야합니다. – alexburtnik
@alexburtnik 감사합니다! 컨트롤러에서 스테퍼 콜백을 처리하는 방법을 알았 으면 테이블은 내가 원하는대로 작동했습니다. – Sonera
당신을 진심으로 환영합니다. viewController에서 뷰 이벤트를 처리하기위한 두 가지 일반적인 접근 방식을 게시했습니다. 아마 그 중 하나를 사용했을 것입니다. – alexburtnik