簡體   English   中英

如何使表格視圖始終加載滾動到頂部?

[英]How do I make a table view always load scrolled to the top?

我正在制作一個應用程序,其中有 2 個表格視圖堆疊在導航 controller 中,並且標題是大標題。 第一個表格視圖中有許多項目,因此當您向下滾動以獲取較低的單元格時,大標題在頂部縮小為較小的標題。 但是,當我然后單擊其中一個較低的單元格時,新的表格視圖以頂部的小導航標題開始,而不是以大標題開始完全向上滾動。 為了使大標題重新出現在我希望頁面開始的配置中,您必須向上滾動。 如何使每個表格視圖都以大標題開頭,而不是匹配之前表格的標題配置?

更新:這是第一個表視圖的代碼以及我如何配置導航 controller。

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        loadCategories()
        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Advert-Bold", size: setLargeTitleSize())!]
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Advert-Bold", size: setSmallTitleSize())!]
        
        
    }
 
    override func viewWillAppear(_ animated: Bool) {
        
        self.navigationController?.navigationBar.isTranslucent = false
        
        self.navigationController?.setStatusBar(backgroundColor: UIColor(hexString: vc1.defaults.string(forKey: "darkest")))
        
        let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor(hexString: vc1.defaults.string(forKey: "almostWhite"))]
        navigationController?.navigationBar.titleTextAttributes = textAttributes
        navigationController?.navigationBar.largeTitleTextAttributes = textAttributes
        
        navigationController?.navigationBar.backgroundColor = UIColor(hexString: vc1.defaults.string(forKey: "darkest"))
        
        self.navigationController?.navigationBar.barTintColor = UIColor(hexString: vc1.defaults.string(forKey: "darkest"))

        
    }
    
    
    //MARK: - TableView Datasource Methods
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return symptoms?.count ?? 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "SymptomCell", for: indexPath)
        
        cell.textLabel?.font = UIFont(name: "Advert-Light", size: setTextSize())
        
        //cell.textLabel?.textColor = UIColor(hexString: vc1.defaults.string(forKey: "darkest"))
        
        cell.backgroundColor = UIColor(hexString: vc1.defaults.string(forKey: "almostWhite"))
        
        tableView.backgroundColor = UIColor(hexString: vc1.defaults.string(forKey: "almostWhite"))
        
        
        
        if let category = symptoms?[indexPath.row] {
            
            cell.textLabel?.text = category.name
        }
        
        
        
        return cell
        
    }
    
    //MARK: - TableView Delegate Methods
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "ToFlows", sender: self) //performs the segue to the list of items associated with that category.
        tableView.deselectRow(at: indexPath, animated: true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! FlowTableViewController
        
        if let indexPath = tableView.indexPathForSelectedRow {
            destinationVC.selectedSymptom = symptoms?[indexPath.row] //this sends the category currently selected over to the selectedCategory variable in the todo list view controller
        }
    }
    
    //MARK: - Data Manipulation Methods
    
    func loadCategories() {
        symptoms = realm.objects(Symptoms.self) //this pulls out all the items with type category, and adds them all to the array of categories.
        
        tableView.reloadData()
    }

嘗試設置prefersLargeTiles

navigationController?.navigationBar.prefersLargeTitles = true

然后設置largeTitleDisplayMode如果它仍然沒有做你需要的

navigationItem.largeTitleDisplayMode = .always

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM