简体   繁体   English

自定义表格单元格未出现

[英]Custom tableview cell is not appearing

I have created a core data model for a sign up page which worked properly but when I attach a table view to that sign up page my custom table cell data is not appearing.我为注册页面创建了一个核心数据模型,该模型可以正常工作,但是当我将表格视图附加到该注册页面时,我的自定义表格单元格数据没有出现。

ViewController.swift视图控制器.swift

import UIKit
import Foundation

class FifthViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet weak var Table1: UITableView!

    var array = ["central","gvk","forum"]

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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = Table1.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

        cell.label1.text = self.array[indexPath.row]

        return cell
    }
}

TableViewCell表视图单元格

import UIKit

class TableViewCell: UITableViewCell {
    @IBOutlet weak var label1: UILabel!
}

Try this it will help you:试试这个它会帮助你:

class FifthViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var Table1: UITableView!

var array = ["central","gvk","forum"]

override func viewDidLoad() {
    super.viewDidLoad()
    Table1.delegate = self
    Table1.dataSource = self
    // Do any additional setup after loading the view.
}

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

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

    cell.label1.text = self.array[indexPath.row]

    return cell
}
}

You forgot to call: Table1.datasource = self and Table1.delegate = self你忘了调用: Table1.datasource = selfTable1.delegate = self

Without calling datasource tableview will not show any data.不调用数据源 tableview 将不会显示任何数据。

我已经连接了数据源和委托,即使当我使用简单的 tableview 而不自定义它时它没有显示数据,它工作正常,所以我想使用重新加载数据。

Try this,试试这个,

class FifthViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet weak var Table1: UITableView!

 override func viewDidLoad(){
    //In View did load  
    super.viewDidLoad()

    // Register the table view cell class and its reuse id
    self.tableView.register(TableViewCell.self, forCellReuseIdentifier:"cell")


    Table1.delegate = self //If not set in story board or Xib
    Table1.datasource = self //If not set in story board or Xib
    var array = ["central","gvk","forum"]
    Table1.reloadData()
    }


     func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
    }
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return array.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell
        if cell==nil
        {
            //initialize the cell here
            cell = TableViewCell.init(style: .Default, reuseIdentifier: "cell")
        }        

        cell.label1.text = self.array[indexPath.row]

        return cell
    }
 }

And before all of this check for identifier in the design of Custom table view cell in story board or xib It should be same as above in cell for row index path并且在所有这些之前检查故事板或xib中的自定义表视图单元格设计中的标识符它应该与上面单元格中的行索引路径相同
Note : This is just pseudo code, might contain error注意:这只是伪代码,可能包含错误

And also you can check full tutorial, here你也可以在这里查看完整的教程

Maybe old but for those who have same problem now.也许老了,但对于那些现在有同样问题的人来说。

For Table view chose Content -> Static cells对于表格视图,选择Content -> Static cells

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM