Tableview在重装数据时崩溃
我有意外的tableviews问题。看起来,每次我尝试重新加载我的表格数据时,该应用程序都会退出而没有错误。我知道,数组是正确形成,所以有什么不对这些功能:Tableview在重装数据时崩溃
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 1 {
return latest.count
}
else if tableView.tag == 2{
return older.count
}
else {
return 0 //In case of error
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell?
if tableView.tag == 1 {
print("Success")
cell = tableView.dequeueReusableCell(withIdentifier: "latestCell")! as UITableViewCell
cell = UITableViewCell(style: UITableViewCellStyle.subtitle,
reuseIdentifier: "latestCell")
cell?.textLabel?.text = latest[indexPath.row]
cell?.detailTextLabel?.text = latestSub[indexPath.row]
cell?.accessoryType = .disclosureIndicator
return cell!
}
else if tableView.tag == 2 {
cell = tableView.dequeueReusableCell(withIdentifier: "olderCell")! as UITableViewCell
cell = UITableViewCell(style: UITableViewCellStyle.subtitle,
reuseIdentifier: "olderCell")
cell?.textLabel?.text = older[indexPath.row]
cell?.detailTextLabel?.text = olderSub[indexPath.row]
cell?.accessoryType = .disclosureIndicator
return cell!
}
else {
return cell!
}
}
之前,这些类型的问题在这里的答案是会忘记设置委托和数据源等...所以我相信这是一个合适的问题。
提前致谢!
编辑:
var latest = [String]()
var older = [String]()
var latestSub = [String]()
var olderSub = [String]()
override func viewDidAppear(_ animated: Bool) {
latestTable.reloadData()
olderTable.reloadData()
}
完整记录;
2017-02-16 15:57:28.889 NotebookApp [24985:604704] Firebase自动屏幕报告已启用。调用+ [FIRAnalytics setScreenName:setScreenClass:]设置屏幕名称或覆盖默认屏幕类名称。要禁用自动屏幕报告,请将Info.plist中的标志FirebaseAutomaticScreenReportingEnabled设置为NO 2017-02-16 15:57:28.997 NotebookApp [24985:] Firebase Analytics v.3600000开始 2017-02-16 15:57:28.999 NotebookApp [24985:]要启用调试日志记录,请设置以下应用程序参数:-FIRAnalyticsDebugEnabled 2017-02-16 15:57:29.012:FIRInstanceID AppDelegate代理已启用,将swizzle应用程序代理远程通知处理程序。要禁用将InfoPlist添加“FirebaseAppDelegateProxyEnabled”并将其设置为NO 2017-02-16 15:57:29.020 NotebookApp [24985:]自动成功创建Firebase Analytics应用程序委托代理。要禁用代理,请在Info.plist中将标志FirebaseAppDelegateProxyEnabled设置为NO 2017-02-16 15:57:29.090 NotebookApp [24985:] AdSupport框架当前未链接。某些功能将无法正常工作。 2017-02-16 15:57:29.180 NotebookApp [24985:]启用Firebase Analytics 2017-02-16 15:57:45.703779 NotebookApp [24985:604704] [LayoutConstraints]无法同时满足约束条件。 以下列表中的至少一个约束可能是您不想要的约束之一。 试试这个: (1)看看每个约束,并试图找出你不期望的; (2)找到添加不需要的约束或约束并修复它的代码。 (注意:如果你看到,你不明白NSAutoresizingMaskLayoutConstraints,请参阅UIView的财产translatesAutoresizingMaskIntoConstraints的文档) ( “”, “” )
将尝试打破约束 恢复
在UIViewAlertForUnsatisfiableConstraints中创建一个符号断点,以在调试器中捕获此断点。 列出的UIView上的UIConstraintBasedLayoutDebugging类中的方法也可能有所帮助。2017-02-16 16:01:34.316879 NotebookApp [24985:604704] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是/ Users/tuomasnummela/Library/Developer/CoreSimulator/Devices/AA87179A-11E5- 4A3A-A63F-B785AA71EC95/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2017-02-16 16:01:34.317476 NotebookApp [24985:604704] [MC]从私有有效用户设置读取。
我有点不认为这里会有什么用处,因为当应用程序退出时,它不会将我带到错误页面事件并突出显示任何代码。
What my project looks like after the app has forcedly closed itself.
可以去错这里的东西是无法找到与您传递在下面的行重用标识符细胞
cell = tableView.dequeueReusableCell(withIdentifier: "olderCell")! as UITableViewCell
所有您需要做的就是注册细胞重用标识符在viewDidLoad中
<olderTableView>.registerClass(UITableViewCell.self, forCellReuseIdentifier: "olderCell") <latestTableView>.registerClass(UITableViewCell.self, forCellReuseIdentifier: "latestCell")
或
可以对mply用以下代替出列和初始化行:
if tableView.tag == 1 {
let reuseId = "latestCell"
let latestCell = tableView.dequeueReusableCell(withIdentifier: reuseId) ?? UITableViewCell(style: .subtitle, reuseIdentifier: reuseId)
latestCell.textLabel?.text = latest[indexPath.row]
latestCell.detailTextLabel?.text = latestSub[indexPath.row]
latestCell.accessoryType = .disclosureIndicator
return latestCell
}
恐怕这也不起作用 – user7356029
@ user7356029我编辑了答案检查应该工作的第二个案例 – Vasanth
没错。你发布的第二个案例就像魅力一样工作。非常感谢! – user7356029
第一个错误是在viewDidAppear
必须调用super.viewDidAppear(animated)
的实现代码如下重装将永远不会被解雇。
这似乎没有什么区别,是否还有第二个错误? – user7356029
缺少其他信息,您发布的日志仅适用于Layout版本 – ciccioska
完整日志现在位于主文章 – user7356029
您在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
中有多个错误。这个崩溃很可能来自于解开cell
的力量,最后一行是零。
这是我的刺伤清理它。请务必在tableView.register(SubtitleCell.self, forCellReuseIdentifier:"latestCell")
的子类中注册UITableViewCell
。除非有其它代码不可见,你可以使用相同的标识符latestCell
和olderCell
- 他们似乎没有从您发布
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell //can't be optional
if tableView.tag == 1 {
print("Success")
cell = tableView.dequeueReusableCell(withIdentifier: "latestCell", forIndexPath: indexPath) as! SubtitleCell
//cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "latestCell") //don't call this here, make a subclass of UITableViewCell to set style
cell.textLabel?.text = latest[indexPath.row]
cell.detailTextLabel?.text = latestSub[indexPath.row]
cell.accessoryType = .disclosureIndicator
return cell
}
else if tableView.tag == 2 {
//same comments as above
cell = tableView.dequeueReusableCell(withIdentifier: "olderCell", for: indexPath) as! SubtitleCell
cell.textLabel?.text = older[indexPath.row]
cell.detailTextLabel?.text = olderSub[indexPath.row]
cell.accessoryType = .disclosureIndicator
return cell
}
else {
cell = tableView.dequeueReusableCell(withIdentifier: "latestCell", forIndexPath: indexPath)
return cell //you were returning nil here - need to return a cell
}
}
下面的代码不同的是如何初始化的一个例子正确的单元格样式
class SubtitleCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
由于代码中的行数为0,所以它不会从最后一行开始崩溃,所以它永远不会返回无格在cellForRowAtIndexPath中 – Vasanth
分享您重新加载表的数组值和方法。 – Nirmalsinh
我也将这些数组添加到另一个函数中,所以如果这似乎是我可以发布的问题。但它包含Firebase,以便永远持续下去。 – user7356029
因此,只要加载视图控制器,您的应用程序就会崩溃,对吗? – Nirmalsinh