简体   繁体   English

表格视图的导航栏下降

[英]Navigation Bar of Table View is going down

I tried to add navigation bar to table view in Main.storyboard. 我试图将导航栏添加到Main.storyboard中的表视图中。 In Main.storyboard, there is no problem navigation bar is seen in the up part of table view. 在Main.storyboard,是没有问题的导航栏在表视图的部分可见。

Also, no problem before data is loaded. 同样,在加载数据之前也没有问题。 Navigation bar is seen in the up part of table view. 导航栏是出现在表视图的一部分

图片1

But, when all data is loaded, navigation bar is going to down . 但是,加载所有数据后,导航栏将关闭

图片2

I want to keep navigation bar in above part of tableview. 我想将导航栏保留在tableview的上方。 Where am I wrong? 我哪里错了? How to solve this problem? 如何解决这个问题呢?

I think, FirstViewController.swift is not important, nevertheless I am sharing all code. 我认为,FirstViewController.swift并不重要,但是我正在共享所有代码。

FirstViewController.swift FirstViewController.swift

import UIKit


class FirstViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var tbCount = 0

    var myMp3s = [Mp3]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        let url = URL(string: "http://example.php")


        URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
            guard let data = data, error == nil else { return }

            do {
                let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]
                let posts = json["server_response"] as? [[String: Any]] ?? []


                for i in posts {

                    print(i["id"]!)

                    let mp3 = Mp3(id: i["id"] as! String, category: i["kategori"] as! String)

                    self.myMp3s.append(mp3)


                }

                self.tableView.reloadData()


                print("counte", self.myMp3s.count)

            } catch let error as NSError {
                print(error)
            }
        }).resume()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myMp3s.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell  = UITableViewCell()

        cell.textLabel?.textAlignment = .center

        cell.textLabel?.text = self.myMp3s[indexPath.row].category

        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(myMp3s[indexPath.row].category)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

尝试这个

This will create a navigation bar 这将创建一个导航栏

The issue is you have drag and drop the NavigationBar from Object library to your ViewController so remove it. 问题是您已将NavigationBarObject library拖放到ViewController因此将其删除。 If you want to show the NavigationBar embed YourViewController to UINavigtionController . 如果要显示NavigationBarUINavigtionController YourViewController嵌入到UINavigtionController

To Embed your ViewController to NavigationConttoller follow this steps. 要将ViewController EmbedNavigationConttoller遵循以下步骤。

  1. Select your ViewController in Interface builder 在“界面”构建器中选择您的ViewController
  2. Go to the Editor menu and select the Embed In. 转到“ Editor菜单,然后选择“嵌入”。
  3. From the new option list select NavigationController 从新选项列表中选择NavigationController

To set title to your Navigation bar , select the Navigation Item for your ViewController from the Document Outline Bar after that go in Attribute Inspector section and set title. 要将title设置为Navigation bar ,请从“ Document Outline Bar为ViewController选择Navigation Item ,然后进入“ Attribute Inspector部分并设置标题。

You can also set title programmatically, add below line in viewDidLoad 您也可以通过编程设置标题,在viewDidLoad添加以下行

self.title = "Title"

在此处输入图片说明

As Per My View Try To set A Navigationbar In a headerview of tableview. 根据我的视图尝试在tableview的headerview中设置导航栏。 that will not move down. 不会下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM