【发布时间】:2017-07-28 17:02:36
【问题描述】:
单击上面的图片可以简要了解我的 Segue。我的应用程序是一个锻炼追踪器。我有一个类,它使锻炼对象具有锻炼名称、描述等属性。我有另一个创建锻炼列表的对象,所以只是一组锻炼对象。
我有一个 UITableView 和它上面的一个按钮,让用户可以创建一个新的锻炼并将其添加到锻炼列表数组中。
每当我单击此“创建新锻炼”时,就会发生崩溃,该“创建新锻炼”会连接到新的视图控制器。我收到此错误
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var setStepper: UILabel!
@IBOutlet weak var repStepper: UILabel!
@IBOutlet weak var workoutName: UITextField!
@IBOutlet weak var workoutDescription: UITextField!
@IBOutlet weak var tableView: UITableView!
var workoutList = WorkoutList().listOfWorkouts
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func stepperCounter(_ sender: UIStepper) {
if sender.tag == 1 {
setStepper.text = "\(Int(sender.value))"
}
else if sender.tag == 2 {
repStepper.text = "\(Int(sender.value))"
}
}
@IBAction func addToWorkout(_ sender: Any) {
//I know this is terrible fixing later. Just for Testing right now
let newWorkout : Workout = Workout(Name: workoutName.text!, Description: workoutDescription.text!, Sets: Int(setStepper.text!)!, Reps: Int(repStepper.text!)!)
workoutList.append(newWorkout)
print(workoutList.count)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return workoutList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Hello"
return cell
}
}
【问题讨论】:
-
如果在崩溃的行上放一个断点并查看
tableView的值呢?是非零吗? -
也许会放上解释崩溃类型的日志
-
我认为可能是由于 cellForRowAt。否则在代码中添加崩溃描述
-
@Maddyヅヅ 我是新开发者:\我如何获取崩溃日志?
-
你的调试器会打印出它的描述“Terminating app due to”
标签: ios iphone uitableview swift3