簡體   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

我是初學者,我正在創建的 iOS 應用程序遇到了一些問題。 我正在利用 SwipeCellKit 包為我的 tableViews 提供可滑動的單元格。 我還想使用自定義單元格來顯示生日。 我創建了一個自定義 tableView 單元格和筆尖。 我遇到的問題是將筆尖正確編碼到我的生日 tableView 控制器中,以便它顯示信息。 下面是我的代碼的圖片。 如果有人能指出我正確的方向,我真的很感激。

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

該應用程序因投射結果而崩潰

tableView.dequeueReusableCell(withIdentifier:for:)

用力解開as! 它返回非可選對象。

要解決錯誤,只需將其更改為as?

還有另一件事也可能導致錯誤,您輸入了單元格的直接標識符,而不是使用標識符BirthdayTableViewCell.identifier

guard let birthdayCell = (tableView.dequeueReusableCell(withIdentifier: BirthdayTableViewCell.identifier, for: indexPath) as? BirthdayTableViewCell) else {fatalError()}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM