我只是从故事板中的自定义单元格加载视图。但在两到三次之后,会发生以下错误
'NSInternalInconsistencyException', reason: 'An instance 0x7ba90000 of class UITableView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7c66ee20> (
<NSKeyValueObservance 0x7c66edf0: Observer: 0x7c68bce0, Key path: contentSize, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x7b633050>
<NSKeyValueObservance 0x7c68e1c0: Observer: 0x7c68bce0, Key path: contentOffset, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x7b609030>
<NSKeyValueObservance 0x7c68e8a0: Observer: 0x7c68bce0, Key path: frame, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x7b60c300>
下面是我写的代码。请看一看,让我知道我错在哪里。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
var identifier = "cell"
if indexPath.row == tblData.count{
identifier = "cell1"
}
var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as UITableViewCell!
if (cell == nil) {
cell = UITableViewCell(style:.Default, reuseIdentifier: identifier)
}
cell.selectionStyle = .None
if identifier == "cell1"{
let cellBtnDownload = cell.viewWithTag(1) as! UIButton
cellBtnDownload.addTarget(self, action: #selector(PlayListDetailController.btnDownload(_:)), forControlEvents: .TouchUpInside)
return cell
}
let cellLbl = cell.viewWithTag(1) as! UILabel
let cellBtn = cell.viewWithTag(2) as! UIButton
cellLbl.text = tblData.objectAtIndex(indexPath.row).valueForKey("t_name") as? String
print(String(indexPath.row))
cellBtn.accessibilityValue = String(indexPath.row)
cellBtn.addTarget(self, action: #selector(PlayListDetailController.BtnPlaySong(_:)), forControlEvents: .TouchUpInside)
return cell
}
看一看
谢谢
最佳答案
尝试删除contentSize、contentOffset或frame的任何自定义观察者。如果要向UITableView或UITableViewCell添加任何
关于ios - UITableView被释放IOS SWIFT随机崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36907849/