繁体   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