Swift - 表格视图中的第二部分不可见 [英] Swift - Second section in tableview not visible

查看:22
本文介绍了Swift - 表格视图中的第二部分不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图给我的控制器两个分组的 tableview 部分.截至目前,第一部分及其标题显示正确,但无处可找到第二部分.这两个部分都非常简单和简短,因此它们应该完全适合我的框架.

I am trying to give my controller two grouped tableview sections. As of now, the first section and its header show up correctly, but the second one is nowhere to be found. Both sections are pretty straightforward and short, so they should perfectly fit within my frame.

第二组去哪儿了,我怎样才能让它出现在第一组下面?

Where did the second group go, and how can I make it appear below the first group?

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
   }
}

推荐答案

numberOfSections(in:) 在 Swift 3 中改变了,所以像这样改变你的方法.

The signature of numberOfSections(in:) is changed in Swift 3, so change your method like way.

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

这篇关于Swift - 表格视图中的第二部分不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆