【问题标题】:student list on button click in swift快速点击按钮上的学生列表
【发布时间】:2016-12-20 03:29:58
【问题描述】:

在我的 main.storyboard 中有“所有学生”按钮。当点击“所有学生”按钮时,我需要打开学生列表。

学生已经保存在领域数据库中。到目前为止,我已经完成了以下工作:-

  1. 已从对象库中拖动视图控制器并放置在空白空间中。
  2. 还在视图控制器中拖动了表格视图和表格视图单元格。
  3. 创建了从“所有学生”按钮到此视图控制器的模态转场。
  4. 已将单元标识符分配给“Cell”并将标识符分配给“rList”

在 vieController 中,我进行了以下更改:-

Class vieController: UIvieController, UITextfieldDelegate

到下面:

Class vieController: UIvieController, UITextfieldDelegate, UITableViewDelegate, UITableViewDatasource.

但在viewDidLoad 中出现以下错误:

致命错误:在展开可选值时意外发现 nil

在下面一行:

table1.dataSource = self

不知道我将如何获得这份学生名单。任何帮助将不胜感激。

【问题讨论】:

  • 能否显示错误截图
  • 请分享你的代码。

标签: ios swift


【解决方案1】:

删除table1.dataSource = self

然后从storyboard进入tableView的Connections Inspectors 之后将 dataSource 拖到您的 viewController

还要确保您的 tableView 插座连接正确。

希望对你有帮助

【讨论】:

    【解决方案2】:

    首先,您应该检查以确保您的视图控制器中有适当的表格视图插座。我在屏幕截图中展示了 table view outlet 是如何创建的。

    然后你可以使用

    self.tableView.dataSource = self 
    

    在你的 viewDidLoad 中。


    就显示数据而言,您可以将数据放入视图控制器中的数组属性中。

    如果数据是从网络加载的,那么应该在数据加载后使用tableView.reloadData()重新加载表格视图。

    您似乎缺少的其他部分是 UITableViewDataSource 所需的数据源方法。

    有两个函数是必需的,它们属于您的视图控制器。

    我在下面提供了最基本的大纲来帮助您入门。

    func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        // The cell identifier is set in your storyboard.
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    
        // Do stuff like set values on labels in the cell.
        cell.label.text = “text to display”
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    {
        // Return the number of cells to be displayed.
        return 10
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      相关资源
      最近更新 更多