【问题标题】:Added a tableview on a viewController, but the data is not there在viewController上添加了一个tableview,但是数据不存在
【发布时间】:2016-05-23 07:52:00
【问题描述】:

我的目标是做一个分组的tableView,但不知何故数据没有添加到tableView中

这是故事板图片

我在视图控制器的顶部添加了一个表视图,它是

我写的代码好像不行

import UIKit
import Alamofire
import SwiftyJSON
import KeychainAccess

class SettingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableView: UITableView!


    let keychain = Keychain(server: "https://genietesting.herokuapp.com", protocolType: .HTTPS)
    var profile: [String]?
    let aboutGenie = [
        "How it works",
        "About",
        "Contact"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        let firstName = keychain[string: "first_name"]
        profile = [
            firstName!
        ]

        tableView.dataSource = self
        tableView.delegate = self

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return profile!.count
        } else {
            return aboutGenie.count
        }
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "Profile"
        } else {
            return "About Genie"
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let tableCell = tableView.dequeueReusableCellWithIdentifier("myCell")
        return tableCell!
    }
}

当然,我想让它成为可点击的,这样它就可以转到自己的 viewController

经过一些建议,我更改了上面的大部分代码,结果还是一样,但这次它显示了标题

结果是

【问题讨论】:

  • 你有没有连接数据源和表视图的委托方法到视图控制器的响应者??
  • 很抱歉问这个问题,我该怎么做?
  • 请查看此链接。 stackoverflow.com/questions/11265039/… 很简单。 yourTableViewName.delegate = self; yourTableViewName.datasource = self;

标签: ios swift uitableview


【解决方案1】:

airsoftFreak,

我可以找出多个错误

  1. 在 ViewController 之上添加的 tableView 没有 IBOutlet。

所以你一定有类似的东西

@IBOutlet var tableView: UITableView!
  1. 您的 SettingsViewController 只确认 UITableViewDataSource 而不是 UITableViewDelegate。如果你想让 didSelectRowAtIndexPath 被触发,你必须确认 UITableViewDelegate

    class SettingsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

  2. 正如许多人在他们的回答中注意到并提到的那样,您必须将您的 viewController 设置为 UITableViewDelegate、UITableViewDataSource 的委托,所以

    self.tableView.dataSource = self

    self.tableView.delegate = self

  3. 你实例化单元格的方式也是错误的 :) Yopu 不应该每次都为每个单元格创建 tableViewCell :) 在 storyBoard 中的 TableView 添加一个原型单元格,按照你想要的方式装饰它并设置reusableIndentifier 为它。假设您设置的 reusableIndentifier 是 'myCell'

您的 cellForRowAtIndexPath 将更改为

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //assuming you have different cells for each section
        switch indexPath.section {
        case 0: let tableCell = tableView.dequeueReusableCellWithIdentifier("myCell")
        tableCell.textLabel.text = profile[indexPath.row]
        return tableCell

            //in swift switch has to be exhaustive so default
        default: let secondSectionCell = tableView.dequeueReusableCellWithIdentifier("second_section_cell_identifier")
        secondSectionCell.textLabel.text =aboutGenie[indexPath.row]
        return secondSectionCell
        }
    }

【讨论】:

  • 谢谢你的回复,我做了你说的一切,除了两个标题外,仍然一无所获。我用你的建议更新了我的问题。
  • @airsoftfreak : 老兄至少有一个标签,并用 aboutGenie 数组的内容更新它的文本。没有办法你不会得到它必须存在的任何单元格,只是你看不到它:) 添加一个标签并设置该标签的文本:) 如果你不想做这么多的编码,只需设置 tableCell.textLabel.text = "嗨”你应该在第二部分看到两个 hi,在第一部分看到一个 :)
  • @airsoftfreak :顺便说一句,额外的信息 :) 如果您的部分标题出现,则意味着您有该部分的单元格:) 如果您声明一个部分标题并将行数设置为 0那么该部分标题将不会出现:) 正如您所指定的,您可以看到两个部分标题,这意味着两个部分中都有单元格:) 只是你看不到它:) 做我在之前的评论中所说的你应该看到你的细胞:)
  • 谢谢,如果我在 tableview 函数中设置标签,它现在会显示,最后一件事,我如何填充表格单元格上的数据?因为我有两个不同的数据元组
  • 我有一个很简单的问题,你如何修改标题的颜色?就像让盒子变大
【解决方案2】:

尝试将 tableview 拖动(ctrl+drag)到 viewcontroller 顶部的黄色按钮。您现在将看到选项:数据源和委托。选择其中一个并再次为另一个执行操作。现在应该将 tableview 链接到您的代码。

如果让它可点击的选项也是一个问题:

使用函数didSelectRowAtIndexpath,您可以实现这一点。应该有很多关于这个问题的堆栈可用。

【讨论】:

    【解决方案3】:

    您可能没有将UITableView delegatedataSource 方法连接到viewController。您可以通过两种方式做到这一点。 1. 以编程方式 创建一个 tableViewOutlet

    override fun viewDidLoad() {
       super.viewDidLoad()
       yourTableViewOutlet.delegate = self
       yourTableViewOutlet.dataSource = self
    }
    
    1. 在界面构建器中

      a) 在情节提要中打开文档大纲。 b) 控制从 tableView 到 ViewController 的拖动。 c) 将delegate和dataSource一一连接。

    点击单元格会触发delegate方法didSelectRowAtIndexPath

    【讨论】:

    • 我根据您的建议更新了我的问题,但仍然没有运气。
    • 您尚未在cellForRowAtIndexPath 方法中向您的单元格添加任何视觉元素。尝试这样做。只需cell.textLabel.Text = yourArray[indexPath.row]cell.backgroundColor = UIColor.greenColor
    【解决方案4】:

    self.tableView.delegate 和 self.tableView.datasource

    override func viewDidLoad() {
        super.viewDidLoad()
        let firstName = keychain[string: "first_name"]
        profile = [
            firstName!
        ]
        self.tableView.delegate = self
        self.tableView.datasource = self
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多