Swift - tableview中的第二部分不可见

时间:2017-03-08 06:24:46

标签: swift uitableview

我试图给我的控制器两个分组的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
   }
}

1 个答案:

答案 0 :(得分:5)

{4}的签名在Swift 3中已更改,因此请改变您的方法。

override func numberOfSections(in tableView: UITableView) -> Int {
    return mySections.count
}
相关问题