Swift 自定义表视图为零
我正在尝试使用VIPER模式实现一个简单的iOS应用程序。该应用程序是一个简单的表视图,显示json请求中的数据 我已经创建了表视图,并且可以使用默认的Swift 自定义表视图为零,swift,uikit,viper,Swift,Uikit,Viper,我正在尝试使用VIPER模式实现一个简单的iOS应用程序。该应用程序是一个简单的表视图,显示json请求中的数据 我已经创建了表视图,并且可以使用默认的单元格.textLabel成功显示来自对象的数据。然而,我试图创建一个自定义的表视图单元格,因此创建了nib和类。我已将所有插座从nib正确连接到该类,该类的代码如下: class CustomTableViewCell: UITableViewCell { static let identifier = "Custo
单元格.textLabel
成功显示来自对象的数据。然而,我试图创建一个自定义的表视图单元格,因此创建了nib和类。我已将所有插座从nib正确连接到该类,该类的代码如下:
class CustomTableViewCell: UITableViewCell {
static let identifier = "CustomTableView"
static func nib() -> UINib {
return UINib(nibName: "CustomTableViewCell", bundle: nil)
}
@IBOutlet var level: UILabel!
@IBOutlet var levelNumber: UILabel!
@IBOutlet var progress: UIProgressView!
@IBOutlet var leftProgress: UILabel!
@IBOutlet var rightProgress: UILabel!
public func configure(levelString: String, levelNum: String, prog: Float, left: String, right: String) {
level.text = levelString
levelNumber.text = levelNum
progress.setProgress(prog, animated: true)
leftProgress.text = left
rightProgress.text = right
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
问题是,当我运行应用程序时,这些出口出错:致命错误:在级别隐式展开可选值时意外发现为零。在configure函数中,text
。我可以通过在视图中执行以下操作来抑制此错误:
table.register(CustomTableViewCell.nib(),forCellReuseIdentifier:CustomTableViewCell.identifier)
但是这只是显示一个空的tableView,没有自定义行或任何数据。如果我执行table.register(CustomTableViewCell.self,forCellReuseIdentifier:CustomTableViewCell.identifier)
则会崩溃。我应该以什么方式进行操作,以及如何获取数据以在tableView中显示?下面增加了相关代码:
let tableView: UITableView = {
let table = UITableView()
table.register(CustomTableViewCell.nib(), forCellReuseIdentifier: CustomTableViewCell.identifier)
table.isHidden = true
return table
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
label.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
label.center = view.center
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return achievements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier, for: indexPath) as! CustomTableViewCell
cell.configure(levelString: "Level",
levelNum: achievements[indexPath.row].level,
prog: Float(achievements[indexPath.row].progress/achievements[indexPath.row].total),
left: String(achievements[indexPath.row].progress),
right: String(achievements[indexPath.row].total))
return cell
}
插座设置:
文件所有者:
我很感激这可能不是每个人的正确答案,但这对我来说是有效的。我一直在努力调试它,为了时间的缘故,我得出结论,以编程方式创建所有东西会更容易。这方面有很多教程,但我采用了以下结构:
用于设置组件约束和添加子视图的甜叶菊库
这在您的视图中被引用,就像它通常被引用一样。我选择将我的添加到封装任何表配置的函数中,然后在viewDidLoad
中调用:
tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: CustomTableViewCell.identifier)
然后对于单元格FORROWAT
:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier) as! CustomTableViewCell
cell.setValues(object: Object[indexPath.row])
return cell
}
仔细检查你的插座。确保文件的所有者
,而不是视图,设置为CustomTableViewCell
。我不确定你的意思,我添加了一个屏幕截图,希望它能让事情变得更清晰,插座看起来很好。但是,单击“文件的所有者”,然后在那里设置类()-您不需要设置主视图的类。添加了截图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier) as! CustomTableViewCell
cell.setValues(object: Object[indexPath.row])
return cell
}