简体   繁体   English

如何在 iOS 中使用自定义 tableview 单元格笔尖,将滑动表视图控制器作为单元格的默认控制器

[英]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.

相关问题 自定义tableview控制器的单元格文本 - custom tableview controller's cell text 如何停止视图控制器以获取tableView单元的子视图的接触? - How to stop the View Controller to get the touch of a tableView cell's subview? 如何在tableView中创建自定义单元格(滑动单元格) - how to create custom cell in tableView (swipe cell) 表单元进入视图控制器 - Table cell into view controller 如何在表格视图单元格上按以向视图控制器显示导航控制器中的文本 - How to press on a tableview cell to present a view controller with the text in navigation controller iOS:如何将表视图单元格的第一个视图控制器标签数据传递给第二个视图控制器的textView? - iOS: How to pass first view controller label data of table view cell to textView of second view controller? 将TableView单元的Firebase数据检索到视图控制器的标签 - Retrieving firebase data of a tableview cell to a view controller's label 自定义表视图单元格未按照笔尖调整大小 - Custom Table view cell not resizing as per nib 带有TableView的子视图控制器笔尖 - Child View Controller Nib with TableView 单击不同的TableView单元格之前,TableView单元格不会加载View Controller - TableView Cell not loading View Controller until Different TableView Cell is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM