[英]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.
导航栏是出现在表视图的一部分了 。
But, when all data is loaded, navigation bar is going to down . 但是,加载所有数据后,导航栏将关闭 。
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.
}
}
The issue is you have drag and drop the NavigationBar
from Object library
to your ViewController
so remove it. 问题是您已将
NavigationBar
从Object library
拖放到ViewController
因此将其删除。 If you want to show the NavigationBar
embed YourViewController
to UINavigtionController
. 如果要显示
NavigationBar
, UINavigtionController
YourViewController
嵌入到UINavigtionController
。
To Embed
your ViewController
to NavigationConttoller
follow this steps. 要将
ViewController
Embed
到NavigationConttoller
遵循以下步骤。
ViewController
in Interface builder ViewController
Editor
menu and select the Embed In. Editor
菜单,然后选择“嵌入”。 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.