【问题标题】:Adding title to TableViewController将标题添加到 TableViewController
【发布时间】:2016-10-04 16:35:09
【问题描述】:

我想弄清楚如何为我的 swift 项目添加一个简单的标题。我已经在表格视图中列出了学生,只想在列表上方的中心添加一个标题,上面写着“学生”,但我一生都想不通。每次我尝试添加任何内容时,它要么妨碍运营商设置,要么不出现。他是我所有的代码,除了一个尝试的标题栏。

class StudentRosterTableViewController: UITableViewController {

    var studentsList = [Dictionary<String, String>]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let studentRoster = ClassRosterModel()
        studentsList = studentRoster.studentRoster
    }

// MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return studentsList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("StudentCell", forIndexPath: indexPath)

        // Configure the cell...
        cell.textLabel!.text = studentsList[indexPath.row]["name"]
        cell.detailTextLabel!.text = studentsList[indexPath.row]["number"]

        return cell
    }

【问题讨论】:

    标签: ios swift uitableview titlebar


    【解决方案1】:

    如果您要创建 UITableViewController 没有 IB(故事板)

    class StudentRosterTableViewController: UITableViewController {
        init() {
            super.init(style: .Plain)
            self.title = "StudentRoster"
        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    使用故事板找到 View Controller 部分并在那里输入您的标题。

    【讨论】:

      【解决方案2】:

      你可以通过实现这个方法来设置章节标题

      func tableView(tableView: UITableView, titleForHeaderInSection section:Int) -> String?
      {
        return "Students"
      }
      

      如果您有多个部分,则可以在部分参数上使用 switch 语句并为每个案例返回适当的标题。

      【讨论】:

      • 如果我这样做,它会添加它,因此它会干扰 Verizon 标签,因为它太高了有没有办法解决这个问题?
      • 你可以在 storyboard 中调整 tableview 的大小来避免这种情况
      • 非常感谢,对不起,如果他们看起来像初学者的问题,试图从我的 java 生活中掌握。
      【解决方案3】:

      设置视图控制器的标题:

       self.title = "some title"
      

      【讨论】:

        【解决方案4】:

        只需添加

        self.title = "Your View controller title"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-26
          • 2013-05-12
          • 2016-06-28
          • 1970-01-01
          相关资源
          最近更新 更多