[英]UITableViewCell doesn't show
i'm creating a simple Table but without storyboard, just coding, currently I can create the table but when I add the cell it crashes, these are my variables: 我正在创建一个简单的表,但没有情节提要,只需编写代码,当前就可以创建表,但是当我添加崩溃的单元格时,这些是我的变量:
var optionsTableView:UITableView!
var options = ["Option1", "Option2"]
When I tapped a button the table appears, the function is: 当我点击一个按钮时,表格就会出现,其功能是:
@IBAction func Options(sender: AnyObject) {
if optionsTableView != nil {
optionsTableView.removeFromSuperview()
self.optionsTableView = nil
return
}
self.optionsTableView = UITableView()
self.optionsTableView.backgroundColor = UIColor(white: 0, alpha: 0.95)
self.optionsTableView.delegate = self
self.optionsTableView.dataSource = self
self.optionsTableView.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint]()
constraints.append(NSLayoutConstraint(item: optionsTableView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 100.0))
constraints.append(NSLayoutConstraint(item: optionsTableView, attribute: .Trailing, relatedBy: .Equal, toItem: sender, attribute: .Trailing, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: optionsTableView, attribute: .Top, relatedBy: .Equal, toItem: sender, attribute: .Bottom, multiplier: 1.0, constant: 5.0))
self.view.addSubview(optionsTableView)
self.view.addConstraints(constraints)
optionsTableView.addConstraints([NSLayoutConstraint(item: optionsTableView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 220.0)])
}
The functions for the table: 该表的功能:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.options.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44.0
}
Up to here everything goes well but with the cellForRowAtIndexPath the app crashes: 到这里为止一切正常,但是使用cellForRowAtIndexPath应用程序崩溃了:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = optionsTableView.dequeueReusableCellWithIdentifier("cell")!
if tableView == optionsTableView {
cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
cell.backgroundView = nil
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.textColor = UIColor.whiteColor()
cell.textLabel?.text = self.options[indexPath.row]
return cell
}
return cell
}
Specifically in this line the app crash: 特别是在此行中,应用程序崩溃:
var cell = optionsTableView.dequeueReusableCellWithIdentifier("cell")!
And console says: "fatal error: unexpectedly found nil while unwrapping an Optional value", what you think is happening?? 并且控制台说:“致命错误:在展开可选值时意外发现nil”,您认为发生了什么??
That line is crashing is because it isn't finding a cell with that reuse identifier. 该行崩溃是因为它找不到具有该重用标识符的单元。
You have to register it: 您必须注册:
self.tableView.registerClass(Cell.self as AnyClass, forCellReuseIdentifier: "cell")
That way when you force unwrap it won't crash. 这样,当您强行打开包装时,它不会崩溃。
I was a little changed your code. 我稍微改变了您的代码。 Fixed constraints.
固定约束。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var optionsTableView = UITableView()
var options = ["Option1", "Option2"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
optionsTableView = UITableView(frame: UIScreen.mainScreen().bounds)
//optionsTableView.backgroundColor = UIColor(white: 0, alpha: 0.95)
optionsTableView.delegate = self
optionsTableView.dataSource = self
optionsTableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(optionsTableView)
optionsTableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
}
override func viewWillLayoutSubviews() {
var constraints = [NSLayoutConstraint]()
constraints.append(NSLayoutConstraint(item: optionsTableView, attribute: .Top, relatedBy: .Equal, toItem: topLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: optionsTableView, attribute: .Bottom, relatedBy: .Equal, toItem: bottomLayoutGuide, attribute: .Top, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: view, attribute: .Trailing, relatedBy: .Equal, toItem: optionsTableView, attribute: .Trailing, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: optionsTableView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1.0, constant: 0))
view.addConstraints(constraints)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.options.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44.0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = optionsTableView.dequeueReusableCellWithIdentifier("cell")!
cell.textLabel?.text = options[indexPath.row]
return cell
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.