【问题标题】:Data loading issue in Table View in IOSIOS表视图中的数据加载问题
【发布时间】:2017-10-18 09:33:04
【问题描述】:

我的视图控制器中有一个表格视图控制器。当我为行索引方法名称提供静态行数和单元格时,它在表格视图中对我没有任何显示。我也重新加载了表格视图,但它没有显示我不知道为什么会这样,

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return messages.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell (style: .subtitle, reuseIdentifier: "cellID")
    let message = messages[indexPath.row]
    cell.textLabel?.text = message.text

    return cell
}

【问题讨论】:

  • 你有没有为你的tableview设置delegate & datasource
  • 你需要设置代理。

标签: ios uitableview swift3


【解决方案1】:

重新检查你是否设置了

tableView.delegate = self
tableView.datasource = self

还要在cellForRowAtIndexpath 上设置一个断点,以检查代码是否贯穿该块。

还要重新检查cellIdentifier(cellID)是否正确。

【讨论】:

  • 非常感谢。你能指导我一个简短的问题吗? @DHEERAJ
  • 当然,这是什么?
  • 我在我的项目中使用了 bridge,我在 NSUserDefault 中存储了字符串的值,现在我想将它传递给我的 swift 类,但是当我传递它时它没有显示值。 @DHEERAJ
【解决方案2】:
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

                //Getting the reference variables of storyboard
                @IBOutlet weak var tableView:UITableView!
                var messages = [String]()

                override func viewDidLoad() {
                    super.viewDidLoad()


                    //Add some packages
                    messages.append("Super")
                    messages.append("Free ")
                    messages.append("Miscellaneous")
                    messages.append("All ")

                    tableView.datasource = self
                    tableView.delegate = self


                }

    func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell (style: .subtitle, reuseIdentifier: "cellID")
        let message = messages[indexPath.row]
        cell.textLabel?.text = message.text

        return cell
    }
}

【讨论】:

  • 谢谢我的回答
【解决方案3】:

使用这个。这个对我有用

  class yourViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
        @IBOutlet weak var tableView:UITableView!

        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.datasource = self
            tableView.delegate = self
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return messages.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell=tableView.dequeueReusableCell(withIdentifier: "cellID",for : indexPath)


            let message = messages[indexPath.row]
            cell.textLabel?.text = message.text

            return (cell)

        }
    }

【讨论】:

    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2015-12-15
    • 1970-01-01
    相关资源
    最近更新 更多