【发布时间】:2020-05-26 20:38:22
【问题描述】:
由于某种原因,变量标签未更新。当 SelectedSauce 运行时,它应该运行函数然后更新变量。但是一旦离开该功能,它就不会被更新。我不确定这有什么问题。当我改变视图时,我将一个变量从前一个视图传递给 selectedsauce 到这里。我不确定它是否有帮助或改变任何东西,但我正在使用领域数据库
class InfoUITableViewController: UITableViewController {
var SelectedSauce: Sauce? {
didSet {
print("1st")
AcquireData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("3rd")
print("Loaded")
tableView.register(UINib(nibName: "DetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "Super")
tableView.dataSource = self
//Returns nill even though i changed the variable in acquiredata
print(Tags)
}
let realm = try! Realm()
var Tags: List<NiceTags>?
//MARK: - Data Manipulation
func AcquireData() {
print("2nd")
if let Sauce = SelectedSauce {
Tags = Sauce.tags
// print(Tags)
}
self.Tags = self.SelectedSauce?.tags
print(self.Tags)
tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("4th")
let cell = tableView.dequeueReusableCell(withIdentifier: "Super", for: indexPath) as! DetailsTableViewCell
//This isn't running, and just uses the default text inside the label
if let TheTags = Tags?[indexPath.row] {
cell.Inflabel.text = TheTags.tags
}
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of row
print("5th")
//Returns 7 because tags is still nill
return Tags?.count ?? 7
}
}
【问题讨论】:
-
SelectedSauce 有标签吗?你检查了吗?
-
lazy var Tags: List<NiceTags>? = List<NiceTags>()也试试这段代码...初始化标签 -
是的,它是我创建的自定义创建的对象/类。当我在函数中运行代码并打印 self.tags 时,控制台中会打印出一个数组
-
在 acquireData 中打印值?
标签: swift xcode storyboard