我有一个包含结构的文件(每个strucht都包含一个数组)。
我希望能够使用用户在另一个视图中选择的Struct!对不起,如果我无法解释自己。
代码如下:
这是包含“锻炼”的内容
import Foundation
struct Routines {
let routineName: String
let videoFileName: String
let description: Array<Any>
let thumbnailFileName: String
static func fetchRoutines() -> [Routines] {
let v1 = Routines(routineName: "Recommended Routine", videoFileName: "v1", description: Workout1.fetchWorkout1(), thumbnailFileName: "v8")
let v2 = Routines(routineName: "Routine Extra", videoFileName: "v2", description: Workout2.fetchWorkout2(), thumbnailFileName: "v2")
return [v1, v2]
}
}
struct Workout1 {
let excerciseName: String
let sets: Int
let reps: Int
static func fetchWorkout1() -> [Workout1] {
let w1 = Workout1(excerciseName: "Bench Press", sets: 3, reps: 8)
let w2 = Workout1(excerciseName: "Push Press", sets: 3, reps: 8)
let w3 = Workout1(excerciseName: "Squat", sets: 3, reps: 8)
let w4 = Workout1(excerciseName: "Deadlift", sets: 3, reps: 8)
let w5 = Workout1(excerciseName: "Bicep Curl", sets: 3, reps: 8)
let w6 = Workout1(excerciseName: "Tricep Pushdown", sets: 3, reps: 8)
return [w1,w2,w3,w4,w5,w6]
}
}
struct Workout2 {
let excerciseName: String
let sets: Int
let reps: Int
static func fetchWorkout2() -> [Workout2] {
let e1 = Workout2(excerciseName: "Bench Press", sets: 5, reps: 15)
let e2 = Workout2(excerciseName: "Push Press", sets: 3, reps: 3)
let e3 = Workout2(excerciseName: "Squat", sets: 5, reps: 5)
let e4 = Workout2(excerciseName: "Deadlift", sets: 3, reps: 3)
let e5 = Workout2(excerciseName: "Bicep Curl", sets: 5, reps: 15)
let e6 = Workout2(excerciseName: "Tricep Pushdown", sets: 5, reps: 15)
return [e1,e2,e3,e4,e5,e6]
}
}
这是ViewController接收信息并显示“锻炼或例程”中某些信息的类。
import Foundation
import UIKit
class WorkoutsListViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.4274509804, green: 0.4745098039, blue: 0.5764705882, alpha: 1)
}
var routines: [Routines] = Routines.fetchRoutines()
var workoutSelected = [Any]()
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return routines.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell", for: indexPath) as! WorkoutsListTableViewCell
let routine = routines[indexPath.row]
cell.routine = routine
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let routine = routines[indexPath.row]
print(routine.routineName)
workoutSelected = routine.description
print(workoutSelected)
/*WorkoutsListViewController.workoutSelected = routine.description
print(WorkoutsListViewController.workoutSelected)
*/
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "segueToWorkout", sender: self)
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
}
这是我希望能够使用该数组并显示信息的地方!
import Foundation
import UIKit
class WorkoutViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = #colorLiteral(red: 0.8352941176, green: 0.8352941176, blue: 0.8352941176, alpha: 1)
}
var workouts: [Workout1] = Workout1.fetchWorkout1()
//var workoutSelected = WorkoutsListViewController.workoutSelected
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return workouts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "workoutCell", for: indexPath)
let workout = workouts[indexPath.row]
cell.textLabel?.text = workout.excerciseName
cell.textLabel?.textColor = UIColor.black
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let workout = workouts[indexPath.row]
tableView.deselectRow(at: indexPath, animated: true)
print("Do \(workout.sets) sets of \(workout.reps) reps of \(workout.excerciseName)")
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.clear
}
}
答案 0 :(得分:-1)
在didSelectRowAt
中将索引路径作为sender
的{{1}}参数传递
performSegue
然后覆盖override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "segueToWorkout", sender: indexPath)
}
,以将锻炼数组传递给目标控制器。
prepare(for segue
顺便说一下,您的结构非常混乱。为什么不对两个锻炼数组都使用一个结构,这样就避免了丑陋的override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToWorkout" {
let destinationController = segue.destination as! WorkoutViewController
let indexPath = sender as! IndexPath
let routine = routines[indexPath.row]
destinationController.workouts = routine.description
}
}
声明
Any
然后在struct Workout {
let excerciseName: String
let sets: Int
let reps: Int
static func fetchWorkout1() -> [Workout] {
return [Workout(excerciseName: "Bench Press", sets: 3, reps: 8),
Workout(excerciseName: "Push Press", sets: 3, reps: 8),
Workout(excerciseName: "Squat", sets: 3, reps: 8),
Workout(excerciseName: "Deadlift", sets: 3, reps: 8),
Workout(excerciseName: "Bicep Curl", sets: 3, reps: 8),
Workout(excerciseName: "Tricep Pushdown", sets: 3, reps: 8)]
}
static func fetchWorkout2() -> [Workout] {
return [Workout(excerciseName: "Bench Press", sets: 5, reps: 15),
Workout(excerciseName: "Push Press", sets: 3, reps: 3),
Workout(excerciseName: "Squat", sets: 5, reps: 5),
Workout(excerciseName: "Deadlift", sets: 3, reps: 3),
Workout(excerciseName: "Bicep Curl", sets: 5, reps: 15),
Workout(excerciseName: "Tricep Pushdown", sets: 5, reps: 15)]
}
}
中声明
Routines
和let description: [Workout]
WorkoutViewController
您必须在某个时候重新加载表视图,例如在var workouts = [Workout]()
中,但永远不要使用viewWillAppear
观察者。
答案 1 :(得分:-1)
解决了!感谢@vadrian。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToWorkout" {
let destinationController = segue.destination as! UINavigationController
let indexPath = sender as! IndexPath
let routine = routines[indexPath.row]
let dvc = destinationController.topViewController as! WorkoutViewController
svc.workouts = routine.description
}
}