无法从具有自定义单元格的dataSource中获取单元格



更新:抱歉,有一个复制/粘贴问题-代码现在显示为存在-所有返回。。。


一般来说,我可以通过所有现有的伟大例子来理解这一点。很乐意更新您可能需要的任何其他信息。

目标:
创建一个UITableViewController,它有两个部分。在上面的部分中,我想显示一个自定义分类行,该行提供基于开关的设置。在下面的部分,我想提供一个项目列表来选择-再次使用自定义类。

所有单元格和链接都在故事板中创建

问题:
它编译良好,但在运行时失败,出现:

由于未捕获的异常"NSInternalConferenceException"而终止应用程序,原因:"UITableView(;layer=;contentOffset:{0,-64};contentSize:{375,88}>)未能从其dataSource()中获取单元格"首次抛出调用堆栈:

我尝试过的:

  1. 使用断点,我看到它滚动通过numberOfSections和numberOfRowsInSection
  2. 我曾尝试使用断点检查cellForRowAtIndexPath中的单元格,但在调用函数之前引发了异常
  3. 断开并重新连接的链接,检查标识符,重新验证我知道要做的一切

类和屏幕截图如下:


带开关的单元类

CellWithSwitch

import UIKit
class CellWithSwitchTableViewCell: UITableViewCell {    
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var nameSwitch: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}

带选择的单元格的类

CellWithSelect

import UIKit
class CellWithSelectTableViewCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}

TableViewController:

class MapOptionsTableViewController: UITableViewController {
var mapOptions = MapOptionsAvailable()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func numberOfSections(in tableView: UITableView) -> Int {
// return the number of sections
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = Int()
if section == 1 {
returnValue = 1  
} else if section == 0 {
returnValue =  1 
}
return returnValue
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("Entered cellforRowAtIndexPath.  Row",indexPath.row,"Section",indexPath.section)
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellWithSwitch", for: indexPath as IndexPath) as! CellWithSwitchTableViewCell
cell.nameLabel.text = "Name of switch"
cell.nameSwitch.setOn(true, animated: true)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellWithSelect", for: indexPath as IndexPath) as! CellWithSelectTableViewCell
cell.nameLabel.text = "Name of Selection"
return cell

}
}
}

如果我能提供更多信息,请告诉我。

应该以前见过这个。

更改旧语法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

到新语法:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

哇!!

最新更新