[英]How to utilize a custom tableview cell nib in iOS with a swipe table view controller as the cell's default controller
I am a beginner and I'm having some issues with an iOS app I'm creating.我是初学者,我正在创建的 iOS 应用程序遇到了一些问题。 I am utilizing the SwipeCellKit package to have swipeable cells for my tableViews.我正在利用 SwipeCellKit 包为我的 tableViews 提供可滑动的单元格。 I would also like to use a custom cell to display birthdays.我还想使用自定义单元格来显示生日。 I created a custom tableView cell and nib.我创建了一个自定义 tableView 单元格和笔尖。 The issue that I'm running into is properly coding the nib into my birthday tableView controller so it will display the information.我遇到的问题是将笔尖正确编码到我的生日 tableView 控制器中,以便它显示信息。 Below is a picture of my code.下面是我的代码的图片。 I'd really appreciate if someone could point me in the right direction.如果有人能指出我正确的方向,我真的很感激。
import UIKit
import RealmSwift
import UserNotifications
class BirthdayTableViewController: SwipeTableViewController {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var birthdayLabel: UILabel!
@IBOutlet weak var age: UILabel!
let realm = try! Realm()
var birthdays: Results<Birthday>?
let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(BirthdayTableViewCell.nib(), forCellReuseIdentifier: BirthdayTableViewCell.identifier)
tableView.rowHeight = 100
tableView.separatorStyle = .none
}
override func viewWillAppear(_ animated: Bool) {
loadBirthdays()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return birthdays?.count ?? 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
guard let birthdayCell = (tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BirthdayTableViewCell) else {fatalError()}
let birthday = birthdays?[indexPath.row]
let firstName = birthday?.firstName ?? ""
let lastName = birthday?.lastName ?? ""
name?.text = firstName + " " + lastName
if let date = birthday?.birthdate as Date? {
birthdayLabel?.text = dateFormatter.string(from: date)
} else {
birthdayLabel.text = " "
}
return cell
}
[Beginning of Code][1]
[TableView Methods][2]
[1]: https://i.stack.imgur.com/fZspG.png
[2]: https://i.stack.imgur.com/9IlD1.png
The app crashes due to casting a result of该应用程序因投射结果而崩溃
tableView.dequeueReusableCell(withIdentifier:for:) tableView.dequeueReusableCell(withIdentifier:for:)
with force unwrap as!
用力解开as!
which returns non optional object.它返回非可选对象。
To solve the error, just change it to as?
要解决错误,只需将其更改为as?
There is another thing which can lead to an error as well, you typed direct identifier of a cell instead using the identifier BirthdayTableViewCell.identifier
还有另一件事也可能导致错误,您输入了单元格的直接标识符,而不是使用标识符BirthdayTableViewCell.identifier
guard let birthdayCell = (tableView.dequeueReusableCell(withIdentifier: BirthdayTableViewCell.identifier, for: indexPath) as? BirthdayTableViewCell) else {fatalError()}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.