我是一名高中學生,使用Swift 3在Xcode 8中編寫應用程序。我試圖創建一種方法來創建具有可摺疊節的表視圖。我正在嘗試創建一種方法讓我的代碼創建一個將所有數據推入組織部分的視圖。我試圖通過使用擴展來顯示我的數據。即使在擴展之後我有一個斷點,它也永遠達不到這一點。swift3擴展函數不叫
編輯:該應用程序的其餘部分是建立在Main.storyboard,但是(如你所見),這tableViewController完全內置代碼。 我可以創建一個WeaponSelectVC的segue,但是沒有顯示任何信息和部分。數據源/委託方法內的斷點不會「中斷」程序,也不會顯示任何信息。
編輯#2:使用按鈕之後的下面的代碼從故事板,我原因請看本TableViewController(WeaponSelectVC)壓:
let window = UIWindow(frame:UIScreen.main.bounds)
window.makeKeyAndVisible()
window.rootViewController = UINavigationController(rootViewController: WeaponSelectVC())
我可以看到加載的主前瞬間的表格。故事板ViewController(帶有按鈕)被加載在此之上。 當我們在一瞬間看到該表格時,該表格不包含任何信息或部分(當我單獨打開該表格時,它也未加載)。
下面是從WeaponSelect類的代碼:
struct section {
var name: String!
var items: [String]!
var collapsed: Bool
init(name: String, items: [String], collapsed: Bool = false) {
self.name = name
self.items = items
self.collapsed = collapsed
}
}
class WeaponSelectVC: UITableViewController {
var sections = [section]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Weapons"
// Initialize the sections array
// Here we have three sections: Mac, iPad, iPhone
sections = [
section(name: "Canadian Weapons", items: ["Colt-browning machine gun M1895/14 (Canadian)","Lee Enfield (Canadian)", "Ross Rifle MK III(Canada)", "Webley revolver (Canadian)", "Lewis (Canadian)", "Mills bomb (Canadian)"]),
section(name: "German Weapons", items: ["KAR-98K (German)", "Mauser Gewehr 98 (German)", "Luger (German), Stick grenade (German)"]), ]
// self.tableView.reloadData()
self.tableView.dataSource = self
// self.tableView.delegate = self
// self.tableView.reloadData()
}
}
extension WeaponSelectVC {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].items.count
}
// Cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return sections[indexPath.section].collapsed ? 0 : 44.0
}
// Header
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
header.titleLabel.text = sections[section].name
header.arrowLabel.text = ">"
header.setCollapsed(sections[section].collapsed)
header.section = section
header.delegate = self
return header
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1.0
}
}
//
// MARK: - Section Header Delegate
//
extension WeaponSelectVC: CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
let collapsed = !sections[section].collapsed
// Toggle collapse
sections[section].collapsed = collapsed
header.setCollapsed(collapsed)
// Adjust the height of the rows inside the section
tableView.beginUpdates()
for i in 0 ..< sections[section].items.count {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}
}
你設置'tableView.dataSource'? – shallowThought