ios - 无法使用委托(delegate)更新 UITableViewCell 中的标签

标签 ios swift uitableview

我的应用程序中有一个食物日记,可以跟踪您每天吃的食物,但我正在努力更新我的 UITableViewCell 中的卡路里标签。一按“保存到美食日记”,我的应用程序就崩溃了,错误如下:

"fatal error: unexpectedly found nil while unwrapping an Optional value"

这是我在 PopUpVC 中的代码(UIViewController,用户可以看到它输入 FoodNameCalories)

protocol AddRowDelegate {
    func didAddRow(name : String, calories : String)
}

class PopupVC: UIViewController {

var delegate: AddRowDelegate?
var section: Int?
var caloriesLabel = " "

var tableData: [String] = [""]

var caloriesCell = caloriesForDiary()
@IBOutlet weak var foodTimeLabel: UILabel!
@IBOutlet weak var foodPopup2: UIView!
@IBOutlet weak var foodPopUp: UIView!
@IBOutlet weak var inputFood: UITextField!
@IBOutlet weak var inputCals: UITextField!

@IBAction func saveToDiary(_ sender: Any) {
    dismiss(animated: true, completion: nil)
    delegate?.didAddRow(name: inputFood.text!, calories: inputCals.text!)
}

然后我将食物和卡路里 UItextFields 的输入传递给我的 FoodDiaryVC :

func didAddRow(name: String, calories: String) {        
    cals.calories.text = calories    
    print(name)
    print(calories)
}   

我添加了显示错误的图片和 ViewController。我不确定为什么它会找到/返回零?我基本上只是试图将我的卡路里标签设置为等于输入(或全天卡路里的累积)

popupVC

error

caloriescell

最佳答案

您的calscalories 对象是nil,导致崩溃。您没有在发布的代码中包含此对象,但请确保在访问它之前实例化该对象。

关于ios - 无法使用委托(delegate)更新 UITableViewCell 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560953/

相关文章:

快速 6 个随机数到 tableViewCell 中的 6 个图像

ios - 启动swift后复制数据库

swift - 为什么我的 TableView scrollToRow 只能部分滚动?

ios - iOS 7 中 UISearchBar 隐藏 View

ios - 每5个单元格更改一次单元格背景

ios - PonyDebugger 没有显示任何网络请求

jquery - Mobile WebKit按钮点击伪类

ios - iOS swift 应用程序启动时出现黑屏

ios - UIScrollView - 像 iOS 7 邮件应用程序一样通过触摸拖动移动键盘?

SWIFT - 为什么我想要发帖时总是出错?