![](/img/trans.png)
[英]Segueing to another view from a custom cell button in SWIFT for ios app
[英]Create a custom cell for iOS app using swift 3
首先,我要疯狂地尝试在iOS中创建自定义单元。 在其他几十个问题的答案之后,我似乎没有得到相同的结果,我也找不到原因。 我的用例:创建一个简单的表视图中使用的自定义单元格。
我有一个坐在UIView内的表格视图,其中的一个单元格带有一个标签:
我给我的手机一个标识符:
我将自定义类链接到情节提要中的单元格:
这是我的视图控制器中的代码:
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
}
}
当运行此命令时,它不返回任何内容,并且在打印时从上述方法返回的单元格为nil:
谁能指出我正确的方向?
谢谢!
你并不需要调用register(_:forCellReuseIdentifier:)
实例方法在你attendeesTableView
。 删除此行应修复未显示的单元格。
原因是您在情节提要板中创建了单元。 或者,如果您在笔尖中设计了它,则需要注册它。
马特·诺伊伯格(Matt Neuberg)在题为“ Programming iOS 10,Seventh Edition”的书中使用情节提要来设计自定义单元格方面有出色而详尽的解释。
这是他的书中的一句话,内容涉及使用故事板时为什么不应该注册:
在表视图控制器代码中,确保不调用register(_:for-CellReuseIdentifier :)。 如果调用它,将告诉运行时不要从情节提要中获取单元格。 (纽伯格,2016年,第469页)
Neuberg,M.(2016年)。 编程iOS 10第7版。 加利福尼亚:O'Reilly Media
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.