【发布时间】:2015-12-06 02:47:54
【问题描述】:
我是一个学习 iOS 和 Swift 的新手,所以提前道歉。目前我正在尝试在 viewController 中设置一个 tableView 并在屏幕的一部分的单元格中显示数据。我当前的问题似乎是在为 numberOfRowsInSection() 调用 viewDidLoad() 中的 Alamofire HTTP 请求后重新加载 tableView 数据。代码如下:
import UIKit
import Alamofire
import SwiftyJSON
class CourseDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var titleLabel: UILabel?
@IBOutlet weak var creditsLabel: UILabel?
@IBOutlet weak var descriptionLabel: UILabel?
@IBOutlet weak var tableView: UITableView!
var detailCourse: Course? {
didSet {
configureView()
}
}
var course: Course!
func configureView() {
self.title = detailCourse?.abbr
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SectionCell")
tableView.delegate = self
tableView.dataSource = self
if let theCourse: Course = self.detailCourse as Course! {
var abbr: String = theCourse.abbr!
APIService.getCourseByAbbr(abbr) { (data) -> Void in
self.course = Course(courseJSON: data)
// Set labels
self.titleLabel?.text = self.course.title!
self.descriptionLabel?.text = self.course.description!
if let creditsArray = self.course.credits {
let minimumCredit = creditsArray[0] as Int
self.creditsLabel?.text = String(minimumCredit)
}
self.tableView.reloadData()
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return course.sections.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCellWithIdentifier("SectionCell", forIndexPath: indexPath) as! SectionTableViewCell
let sectionCell = course.sections[indexPath.row]
cell.termLabel?.text = sectionCell.term
cell.timeLabel?.text = sectionCell.startTime
cell.instructorLabel?.text = sectionCell.instructor
return cell
}
}
当我运行时,我收到以下错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我认为原因可能是我在viewController中设置了tableView不正确。
对于完整的项目,这里是 repo 的链接:https://github.com/classmere/app/tree/develop
【问题讨论】:
-
您在哪一行收到错误消息?还要确保在重新加载 tableView 时您的“numberOfRowsInSection”是正确的。
-
@air6199 返回 course.sections.count
-
现在把那行注释掉,然后用“return 0”替换它,会发生什么?我试图访问您的项目,但它缺少此文件。
-
我想我已经找出问题的根源了...... self.detailCourse 和 self.course 都是零。他们从来没有被分配任何东西。您的“if let theCourse: ...”语句永远不会正确,因此它无法运行。这会检查它是否确实存在,但它返回 false,因为它当前为 nil。不完全确定我是否正确,但看起来就是这样。
-
@air6199 对于您的第一条评论,它会运行,但不会显示任何数据。对于您的第二条评论,我认为它们为零,因为它们没有机会被初始化,因为 APIService 中的代码没有完成及时返回对象以供 tableView 方法使用。我相信这可能是因为 self.tableView.reloadData() 指向不正确,因为 viewController 中的 tableView 设置不正确。
标签: ios swift uitableview delegates datasource