如何使用iOS中的自定义tableview单元格nib,将滑动表视图控制器作为单元格';s默认控制器
我是一个初学者,我正在创建的iOS应用程序有一些问题。我正在利用SwipeCellKit包为我的表视图提供可切换单元格。我还想使用自定义单元格来显示生日。我创建了一个自定义的tableView单元格和nib。我遇到的问题是将nib正确编码到我的生日tableView控制器中,以便它显示信息。下面是我的代码的图片。如果有人能给我指出正确的方向,我将不胜感激如何使用iOS中的自定义tableview单元格nib,将滑动表视图控制器作为单元格';s默认控制器,ios,uitableview,Ios,Uitableview,我是一个初学者,我正在创建的iOS应用程序有一些问题。我正在利用SwipeCellKit包为我的表视图提供可切换单元格。我还想使用自定义单元格来显示生日。我创建了一个自定义的tableView单元格和nib。我遇到的问题是将nib正确编码到我的生日tableView控制器中,以便它显示信息。下面是我的代码的图片。如果有人能给我指出正确的方向,我将不胜感激 import UIKit import RealmSwift import UserNotifications class Birthday
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
导入UIKit
导入RealmSwift
导入用户通知
类BirthdayTableViewController:SwipeTableViewController{
@IBVAR名称:UILabel!
@IBVAR生日标签:UILabel!
@IBVAR年龄:UILabel!
让realm=try!realm()
var生日:结果如何?
让dateFormatter=dateFormatter()
重写func viewDidLoad(){
super.viewDidLoad()
register(BirthdayTableViewCell.nib(),forCellReuseIdentifier:BirthdayTableViewCell.identifier)
tableView.rowHeight=100
tableView.separatorStyle=.none
}
覆盖函数视图将出现(uo动画:Bool){
生日
}
重写func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回生日?计数??1
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=super.tableView(tableView,cellForRowAt:indexPath)
保护让birthdayCell=(tableView.dequeueReusableCell(带有标识符:“Cell”,for:indexath)作为!BirthdayTableViewCell)else{fatalError()}
让生日=生日?[indexath.row]
让firstName=生日?.firstName??“”
让lastName=生日?.lastName??“”
名称?.text=firstName+“”+lastName
如果让日期=生日?生日作为日期{
birthdayLabel?.text=dateFormatter.string(起始日期:日期)
}否则{
birthdayLabel.text=“”
}
返回单元
}
[守则开始][1]
[TableView方法][2]
[1]: https://i.stack.imgur.com/fZspG.png
[2]: https://i.stack.imgur.com/9IlD1.png
应用程序因强制执行以下操作而崩溃:
tableView.dequeueReusableCell(标识符:for:)
使用force unwrapas!
返回非可选对象
要解决此错误,只需将其更改为as?
还有一件事也可能导致错误,您键入了单元格的直接标识符,而不是使用标识符BirthdayTableViewCell.identifier
guard let birthdayCell = (tableView.dequeueReusableCell(withIdentifier: BirthdayTableViewCell.identifier, for: indexPath) as? BirthdayTableViewCell) else {fatalError()}
应用程序因投出以下结果而崩溃: tableView.dequeueReusableCell(标识符:for:) 使用force unwrap
as!
返回非可选对象
要解决此错误,只需将其更改为as?
还有一件事也可能导致错误,您键入了单元格的直接标识符,而不是使用标识符BirthdayTableViewCell.identifier
guard let birthdayCell = (tableView.dequeueReusableCell(withIdentifier: BirthdayTableViewCell.identifier, for: indexPath) as? BirthdayTableViewCell) else {fatalError()}
它会使应用程序崩溃还是工作正常?使用上面的代码会使应用程序崩溃。如果我去掉防护,请将birthdayCell=(tableView.dequeueReusableCell(带有标识符:“Cell”,for:indexPath)设为!BirthdayTableViewCell)或者{fatalError()}它工作正常。条件绑定的初始值设定项必须具有可选类型,而不是“BirthdayTableViewCell”。这是我得到的错误。它会使应用程序崩溃还是工作正常?它会使应用程序崩溃,代码与上面的代码相同。如果我取消防护,请让birthdayCell=(tableView.DequeueableReuseCell(使用标识符:“Cell”,for:indexath)作为!BirthdayTableViewCell)else{fatalError()}它工作正常。条件绑定的初始值设定项必须具有可选类型,而不是“BirthdayTableViewCell”。这是我得到的错误。感谢对该错误的帮助:但是,它仍然不返回单元格中的任何信息。它返回空单元格。感谢对该错误的帮助:但是,它仍然不返回任何信息返回一个空单元格。