我想使用包含文本字段的自定义单元格类。但我总是得到每个单元格的DynamicCastClassUnconditional错误。我该如何摆脱这个错误?我的Custom Cell课程是否正确?
表级
import UIKit
class SettingsEndpointCreateViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellId:String = "EndPointName";
var cell:TextCell = tableView.dequeueReusableCellWithIdentifier(cellId) as TextCell;
return cell;
}
@IBAction func returnToPrevious(){
self.dismissViewControllerAnimated(true, completion: nil);
}
}
定制细胞类
import UIKit
class TextCell: UITableViewCell {
var textField:UITextField = UITextField(frame: CGRectMake(0, 0, 100, 100))
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
addSubview(textField)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
答案 0 :(得分:1)
您需要使用该重用标识符的表视图注册自定义单元类。在视图控制器的viewDidLoad
中,添加以下内容:
class SettingsEndpointCreateViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(TextCell.self, forCellReuseIdentifier: "EndPointName")
}
// ...
}
另请注意,您用于使单元格出列的方法会返回一个可选值。您可以处理它或调用较新的非可选返回方法:
let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as TextCell
(最后,重用标识符应该是某个常量集。)