通常,我们在ViewController类中使用dequeueReuseableCellwithIdentifier方法,但我想在UITableViewCell中使用此方法。我已尝试但是我得到了这样的异常。
fatal error: unexpectedly found nil while unwrapping an optional value
ViewController类:
@IBOutlet var tableView: UITableView!
var tableData:[songData] = [songData]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = UIColor.orangeColor()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = TableViewCell()
cell.datas()
return cell
}
}
TableViewCell类:
@IBOutlet var text1: UILabel!
@IBOutlet var text2: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func datas(){
let vc = ViewController()
let tableData = vc.tableData
print(tableData)
let tableview = vc.tableView
let indexpath:NSIndexPath = NSIndexPath()
let cell = tableview.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexpath) as! TableViewCell //The fatal error is showing exactly at this line.
let artistAndAlbum = tableData[indexpath.row]
cell.text1.text = artistAndAlbum.country
cell.text2.text = artistAndAlbum.currency
tableview.reloadData()
}
我需要在TableViewCell类中自定义表数据。如果有可能帮助我,或者为什么它不可能?
答案 0 :(得分:0)
在视图控制器的viewDidLoad()
中,使用重用标识符注册该类。
tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "ID")
然后,您的cellForRowAtIndexPath
方法可以将自定义单元格出列。
tableView.dequeueReuseableCellWithIdentifier("ID", indexPath: indexPath)
这不仅限于视图控制器。如果您有自定义表格视图单元格,则在设置表格视图的任何位置注册该类以获取重用标识符,然后在cellForRowAtIndexPath
中使用该标识符将自定义单元格出列。
作为一般经验法则,您的视图不应该保留对其视图控制器的引用。视图不应关心任何视图控制器或需要知道视图控制器正在做什么。整个表视图及其所有工作应该在视图中,从视图控制器隐藏,或者您应该将所有表视图代码保留在视图控制器中。这将使您的生活更轻松。
答案 1 :(得分:0)
在cellForRowAtIndexPath
方法之后使用此代码: -
对于自定义单元格
CustomCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
//-------------------------------------------------------------
正常细胞
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
答案 2 :(得分:0)
检查vc.tableView
。它可能是nil
答案 3 :(得分:0)
你这是错误的方式。老实说,你的表格单元子类创建自己没有任何意义。但是,确实有意义的是,您的单元子类要传递数据,并且要从中填充数据。
您应该让视图控制器像往常一样将单元格出列,然后更改表格单元格函数以将一些数据作为参数并自行更新。
在视图控制器中:
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "INSERT_NIB_NAME", bundle: nil), forCellReuseIdentifier: "Cell")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
cell.updateWithData(tableData[indexPath.row])
return cell
}
如果您的单元格是故事板中的原型单元格,那么您必须在那里设置重用标识符,而不是在viewDidLoad
中注册。
在您的表格单元格中:
func updateWithData(artistAndAlbum: songData) {
text1.text = artistAndAlbum.country
text2.text = artistAndAlbum.currency
}