繁体   English   中英

ios swift11 tableView不显示自定义单元格

[英]ios swift11 tableView not showing custom cells

我是快速学习的新手。 当我运行我的代码时,我希望在一个单元格中看到一个电子邮件地址,其右侧有一个详细信息披露。 相反,我看到空白单元格。

主故事板

这就是我的主要故事板的样子

这就是空单元格的样子

AddressBookTableViewController.swift

    import UIKit

        class AddressBookTableViewController: UITableViewController {
        let dataSource = ContactEmailDataSource()
        override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar 
        for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        }

        // MARK: - Table view data source

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

    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return dataSource.countOfEmails()
    }

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

        let cellContact = dataSource.emailAtIndex(index: indexPath.row)
        cell.textLabel?.text = cellContact.emailAddress
        
        return cell
    }
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

联系电子邮件.swift

    import UIKit

class ContactEmail: NSObject
{
    var emailAddress: String
    
    override init ()
    {
        emailAddress = "example@email.com"
        super.init()
    }
    
    init(emailAddress email: String)    
    {
        emailAddress = email
        super.init()
    }
}

ContactEmailDataSource.swift

    import UIKit

class ContactEmailDataSource: NSObject
{
    let emailAddresses = NSMutableArray()
    
    override init()
    {
        super.init()
        loadEmailAddresses()
    }
    func loadEmailAddresses()
    {
        let sample1 = ContactEmail()
        emailAddresses.add(sample1)
        let sample2 = ContactEmail(emailAddress: "example@example.com")
        addEmail(contact: sample2)
    }
    func addEmail(contact c: ContactEmail)
    {
        emailAddresses.add(c)
    }
    func countOfEmails() -> Int
    {
        return emailAddresses.count
    }
    func emailAtIndex(index i: Int) -> ContactEmail
    {
        return emailAddresses.object(at: i) as! ContactEmail
    }
}

如果需要更多屏幕截图或更多信息,请告诉我。 谢谢。

看起来您忘记为表格视图控制器分配自定义类

您的图像显示了这一点:

在此处输入图像描述

但它应该看起来像这样:

在此处输入图像描述

您是否将单元格注册到表格视图? 看看这个链接

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")

暂无
暂无

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

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