在Swift中初始化可选的UIActivityIndi​​catorView

时间:2017-06-14 21:18:56

标签: swift xcode storyboard

我不知道我做错了什么。在我的TableViewController中,我希望每个单元格在点击时都有一个UIActivityIndi​​cator。所以在我的TableViewCell类中,我有以下内容:

@IBOutlet weak var loadingIcon: UIActivityIndicatorView!

在TableViewController中,我有以下内容:

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

    let cellIdentifier = "taskCell"

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TodayTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TodayTableViewCell.")
    }

    let task = tasks[indexPath.row]

    cell.nameLabel.text = task.name
    cell.descriptionLabel.text = task.description
    cell.timeRangeLabel.text = task.timeRangeLabel
    cell.currentTime.text = String(format:"%f", task.currentTime)
    cell.totalTime.text = String(format:"%f", task.totalTime)
    cell.taskIcon.image = task.icon
    cell.loadingIcon = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
    cell.loadingIcon.hidesWhenStopped = true

    return cell
}

override  func tableView(_ tableView: UITableView, didSelectRowAt
    indexPath: IndexPath){

    let cell = (TodayTableViewCell)();tableView.cellForRow(at: indexPath)
    print("tapped")
    tasks[indexPath.row].isActive = !tasks[indexPath.row].isActive
    if(tasks[indexPath.row].isActive) {
        //Loading.start()
        cell.loadingIcon.startAnimating()
    }
    else {
        //Loading.stop()
        cell.loadingIcon.stopAnimating()
    }
}

我得到fatal error: unexpectedly found nil while unwrapping an Optional value但我不知道为什么,因为它看起来像我正在初始化它。

我明白了:

EXC_BAD_INSTRUCTION

这是故事板:

enter image description here

enter image description here

现在回到我之前收到的错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

正如您在图片中看到的那样,您可以在故事板上设置该属性。您可以在xcode的右侧面板中找到。

如果您仍想写该行,则应将UIActivityIndicatorIBOutlet

相关联

enter image description here

相关问题