简体   繁体   English

使用Swift 3为iOS应用创建自定义单元格

[英]Create a custom cell for iOS app using swift 3

First of all, I'm going insane trying to create a custom cell in iOS. 首先,我要疯狂地尝试在iOS中创建自定义单元。 Following dozens of other question answers, I don't seem to be getting the same result and I can't find out why. 在其他几十个问题的答案之后,我似乎没有得到相同的结果,我也找不到原因。 My use case: create a custom cell used in a table view, simple. 我的用例:创建一个简单的表视图中使用的自定义单元格。

I have a table view sitting inside a UIView, with a single cell with a single label: 我有一个坐在UIView内的表格视图,其中的一个单元格带有一个标签: 在此处输入图片说明

I gave my cell an identifier: 我给我的手机一个标识符:

在此处输入图片说明

I link a custom class to the cell in the storyboard: 我将自定义类链接到情节提要中的单元格:

在此处输入图片说明

I link the label in the cell to an outlet on my custom class: 我将单元格中的标签链接到我的自定义类的插座上: 在此处输入图片说明

Here is the code from my view controller: 这是我的视图控制器中的代码:

    class LoggedInViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
        @IBOutlet weak var tmImage: UIImageView!
        @IBOutlet weak var tmNameLabel: UILabel!
        @IBOutlet weak var tmMeetingLabel: UILabel!
        @IBOutlet weak var mainLabel: UILabel!
        @IBOutlet weak var attendeesTableView: UITableView!

        //Test table view details
        let names: [String] = ["Bobby", "Mark", "Waldo"]

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib

            //Setup table and cells
            attendeesTableView.delegate = self
            attendeesTableView.dataSource = self
            self.attendeesTableView.register(LoggedInTableViewCell.self, forCellReuseIdentifier: "attendeeCell")
        }

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

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell: LoggedInTableViewCell = self.attendeesTableView.dequeueReusableCell(withIdentifier: "attendeeCell", for: indexPath) as! LoggedInTableViewCell
            cell.cellNameLabel?.text = self.names[indexPath.row]
            return cell
        }
    }

And when this is ran, it returns nothing and the cell being returned from the method above is nil when printing: 当运行此命令时,它不返回任何内容,并且在打印时从上述方法返回的单元格为nil:

在此处输入图片说明

Can anyone point me in the right direction? 谁能指出我正确的方向?

Thanks! 谢谢!

You do not need to call the register(_:forCellReuseIdentifier:) instance method on your attendeesTableView . 你并不需要调用register(_:forCellReuseIdentifier:)实例方法在你attendeesTableView Removing this line should fix the cells not displaying. 删除此行应修复未显示的单元格。

The reason for this is you created the cell in the storyboard itself. 原因是您在情节提要板中创建了单元。 Alternatively, if you designed it in a nib you would need to register it. 或者,如果您在笔尖中设计了它,则需要注册它。

Matt Neuberg has an excellent and detailed explanation on designing a custom cell using the storyboard in his book titled "Programming iOS 10, Seventh Edition". 马特·诺伊伯格(Matt Neuberg)在题为“ Programming iOS 10,Seventh Edition”的书中使用情节提要来设计自定义单元格方面有出色而详尽的解释。

Here is a quote from his book regarding why you should not register if you are using the storyboard: 这是他的书中的一句话,内容涉及使用故事板时为什么不应该注册:

In your table view controller code, make sure you do not call register(_:for- CellReuseIdentifier:). 在表视图控制器代码中,确保不调用register(_:for-CellReuseIdentifier :)。 If you do call it, you will be telling the runtime not to get the cell from the storyboard. 如果调用它,将告诉运行时不要从情节提要中获取单元格。 (Neuberg, 2016, p. 469) (纽伯格,2016年,第469页)

Neuberg, M. (2016). Neuberg,M.(2016年)。 Programming iOS 10 7th Edition. 编程iOS 10第7版。 California: O'Reilly Media 加利福尼亚:O'Reilly Media

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

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