【问题标题】:Variable not updating inside the view变量未在视图内更新
【发布时间】: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&lt;NiceTags&gt;? = List&lt;NiceTags&gt;() 也试试这段代码...初始化标签
  • 是的,它是我创建的自定义创建的对象/类。当我在函数中运行代码并打印 self.tags 时,控制台中会打印出一个数组
  • 在 acquireData 中打印值?

标签: swift xcode storyboard


【解决方案1】:

像这样从标签栏中设置 SelectedSauce

override func viewDidLoad() { 
super.viewDidLoad() 
swipeAnimatedTransitioning?.animationType = SwipeAnimationType.sideBySide 
// isCyclingEnabled = true 
// Do any additional setup after loading the view. 
print("order") 
let first_vc = self.viewControllers?.first as! DetailViewController 
let last_vc = self.viewControllers?.last as! InfoUITableViewController 
first_vc.SelectedSauce = SelectedSauce 
last_vc.SelectedSauce = SelectedSauce 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2017-05-18
    • 2023-03-17
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多