2017-03-08 52 views
0

我想給我的控制器兩個分組的tableview部分。截至目前,第一部分及其標題顯示正確,但第二部分無處可查。這兩個部分都非常簡單和簡短,所以他們應該完全適合我的框架。Swift - 在tableview中的第二部分不可見

第二組去哪兒,我怎麼能讓它出現在第一組以下?

import UIKit 

struct SectionData { 
    let title: String 
    let data: [String] 

var numberOfItems: Int { 
    return data.count 
} 

subscript(index: Int) -> String { 
    return data[index] 
    } 
} 

extension SectionData { 
    init(title: String, data: String...) { 
    self.title = title 
    self.data = data 
    } 
} 

class SettingsController: UITableViewController { 

let cellId = "cellId" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.dataSource = self 
    self.tableView.delegate = self 
    self.tableView.isScrollEnabled = false 
    self.tableView.tableFooterView = UIView(frame: CGRect.zero) 
    self.tableView.backgroundColor = UIColor(white: 0.95, alpha: 1) 
    self.tableView.rowHeight = 60 
} 

lazy var mySections: [SectionData] = { 
    let section1 = SectionData(title: "INFORMATION", data: "About the app", "Our website", "Twitter", "Call us") 
    let section2 = SectionData(title: "SUPPORT", data: "Report a problem", "Privacy Policy") 

    return [section1, section2] 
}() 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return mySections.count 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return mySections[section].numberOfItems 
} 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return mySections[section].title 
} 

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.font = UIFont(name: "Futura", size: 16)! 
    header.textLabel?.textColor = UIColor.darkGray 
} 

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 40 
} 

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

    let cellTitle = mySections[indexPath.section][indexPath.row] 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as UITableViewCell 
    cell.selectionStyle = UITableViewCellSelectionStyle.default 
    cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator 
    cell.textLabel?.text = cellTitle 

    return cell 
    } 
} 

回答

5

numberOfSections(in:)簽名就在斯威夫特3改變,因此改變你的方法類似的方式。

override func numberOfSections(in tableView: UITableView) -> Int { 
    return mySections.count 
} 
+0

非常感謝!現在正在工作。 Xcode沒有通知我這種奇怪的東西。它通常會告訴我在Swift 3中更改一些設置。將此標記爲答案。 –

+0

@DennisvanMazijk歡迎伴侶:) –