簡體   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