繁体   English   中英

自定义表格单元格未出现

[英]Custom tableview cell is not appearing

我为注册页面创建了一个核心数据模型,该模型可以正常工作,但是当我将表格视图附加到该注册页面时,我的自定义表格单元格数据没有出现。

视图控制器.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
    }
}

表视图单元格

import UIKit

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

试试这个它会帮助你:

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
}
}

你忘了调用: Table1.datasource = selfTable1.delegate = self

不调用数据源 tableview 将不会显示任何数据。

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

试试这个,

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
    }
 }

并且在所有这些之前检查故事板或xib中的自定义表视图单元格设计中的标识符它应该与上面单元格中的行索引路径相同
注意:这只是伪代码,可能包含错误

你也可以在这里查看完整的教程

也许老了,但对于那些现在有同样问题的人来说。

对于表格视图,选择Content -> Static cells

暂无
暂无

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

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