环境:
但是,如果我有大量的细胞,例如:20,那么我试图避免的问题就会立即显现出来:
将初始视图控制器(带按钮的控制器)嵌入到`UINavigationController`中。
我看不出怎么做,因为我似乎能够在导航控制器中嵌入的唯一东西是标签栏控制器
如果尚未创建,请创建一个`UITableViewController`类并将其链接到表视图控制器。
我已经将class ExistingLocationsViewController: UITableViewController { ... }
(以及class ExistingLocationTableViewCell: UITableViewCell { ... }
)链接起来,我相信,正确
将此代码添加到类中,以便在查看表视图时使工具栏显示和消失:
import UIKit class ViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() navigationController?.isToolbarHidden = false } override func viewWillDisappear(_ animated: Bool) { navigationController?.isToolbarHidden = true } }这应该是你所要做的。如果要编辑工具栏,请在编辑时将其显示,然后再将其隐藏。
仅供参考,在我的Xcode,iOS版本中,该属性似乎被命名为toolbarHidden
(不是 **isT**oolbarHidden
)
答案 0 :(得分:5)
向任何视图添加工具栏的最简单方法之一是将其嵌入NavigationViewController
。所有这一切都可以在没有代码的情况下完成:
选择您当前拥有的视图控制器(我假设UITableViewController
)并在编辑器 - >下嵌入选择导航控制器。
修改强> 由于您有一个不同的初始视图控制器,我建议您稍微区别地解决问题。这需要一些代码:
将初始视图控制器(带按钮的控制器)嵌入UINavigationController
。删除任何其他导航控制器。您的按钮应该只有一个显示UITableViewController
的segue。禁用导航控制器中的工具栏和以前一样,由您决定启用导航栏(我建议它)。
如果尚未创建,请创建一个UITableViewController
类并将其链接到Table View Controller。
将此代码添加到类中,以便在查看表视图时使工具栏显示和消失:
import UIKit
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isToolbarHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.isToolbarHidden = true
}
}
这应该是您必须做的所有事情。如果要编辑工具栏,请在编辑时使其可见,然后再将其隐藏。
答案 1 :(得分:1)
解决方案(感谢Christoph!很多)
初步工作
UIViewController
(ViewController.swift
)组成的单页应用。 UITableViewController
并为其创建了我自己的自定义类(MyTableViewController.swift
)MyTableViewCell.swift
)UIViewController
按钮到UITableViewController
中的表格创建了segues
Identifier
(稍后将使用它,但现在允许将id从初始视图传递到表视图)修复 - a.k.a. Christoph to the Rescue
代码
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var b1: UIButton!
@IBOutlet weak var b2: UIButton!
@IBOutlet weak var b3: UIButton!
var buttonId: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
b1.titleLabel?.adjustsFontSizeToFitWidth = true
b2.titleLabel?.adjustsFontSizeToFitWidth = true
b3.titleLabel?.adjustsFontSizeToFitWidth = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destination = segue.destinationViewController as! MyTableViewController
destination.buttonId = segue.identifier
}
}
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var l1: UILabel!
@IBOutlet weak var l2: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Add bordering to bottom of each cell
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor.darkGrayColor().CGColor
border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height)
border.borderWidth = width
self.layer.addSublayer(border)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
import UIKit
class MyTableViewController: UITableViewController {
@IBOutlet weak var b1: UIBarButtonItem!
@IBOutlet weak var b2: UIBarButtonItem!
var buttonId: String!
var oldRow = -1
// The data below is clearly just for testing, eventually this should be pulled from file or DB
var locationList: [LocationObject] = [
LocationObject(name: "name-01", address: "addr-1", selected: false),
LocationObject(name: "name-02", address: "addr-2", selected: false),
LocationObject(name: "name-03", address: "addr-3", selected: false),
LocationObject(name: "name-04", address: "addr-3", selected: false),
LocationObject(name: "name-05", address: "addr-3", selected: false),
LocationObject(name: "name-06", address: "addr-3", selected: false),
LocationObject(name: "name-07", address: "addr-3", selected: false),
LocationObject(name: "name-08", address: "addr-3", selected: false),
LocationObject(name: "name-09", address: "addr-3", selected: false),
LocationObject(name: "name-10", address: "addr-3", selected: false),
LocationObject(name: "name-11", address: "addr-1", selected: false),
LocationObject(name: "name-12", address: "addr-2", selected: false),
LocationObject(name: "name-13", address: "addr-3", selected: false),
LocationObject(name: "name-14", address: "addr-3", selected: false),
LocationObject(name: "name-15", address: "addr-3", selected: false),
LocationObject(name: "name-16", address: "addr-3", selected: false),
LocationObject(name: "name-17", address: "addr-3", selected: false),
LocationObject(name: "name-18", address: "addr-3", selected: false),
LocationObject(name: "name-19", address: "addr-3", selected: false),
LocationObject(name: "name-10", address: "addr-3", selected: false),
]
// This embedded class is to support the above data. It may get replaced in final code
class LocationObject {
var name: String!
var address: String!
var selected: Bool!
init(name: String, address: String, selected: Bool) {
self.name = name
self.address = address
self.selected = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
// code to hide/expose the navbar (top) and toolbar(bottom) when the view loads
navigationController?.navigationBarHidden = false
navigationController?.toolbarHidden = true
}
// code to hide the navbar and toolbar when this view goes away
override func viewWillDisappear(animated: Bool) {
navigationController?.navigationBarHidden = true
navigationController?.toolbarHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locationList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("resultCell", forIndexPath: indexPath) as! MyTableViewCell
let row = indexPath.row
let item = locationList[row]
cell.l1.text = item.name
cell.l2.text = item.address
return cell
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyTableViewCell
let row = indexPath.row
cell.accessoryType = .None
locationList[row].selected = false
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
b1.enabled = true
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyTableViewCell
let row = indexPath.row
locationList[row].selected = !locationList[row].selected
cell.accessoryType = (locationList[row].selected == true) ? .Checkmark : .None
cell.highlighted = false
cell.setHighlighted(false, animated: true)
// TOGGLE SELECTION
if oldRow == row {
b1.enabled = false
let oldIndexPath = NSIndexPath(forRow: oldRow, inSection: 0)
tableView.deselectRowAtIndexPath(oldIndexPath, animated: true)
oldRow = -1
}
else {
oldRow = row
}
}
}
而且,简而言之就是这样。我仍然处于开发我的应用程序的早期阶段 - 不知何故,我有理由相信,在我前进的过程中,我会发布更多有关实施的问题,但希望上述内容可以帮助其他可能正在努力解决问题的人,或类似的,问题。
再次感谢 Christoph 提供有关将视图嵌入导航视图控制器等方面的必要指示。