【问题标题】:how to customizing swift firabase didselectedrowat如何自定义 swift firebase didselectrowat
【发布时间】:2022-01-03 21:45:28
【问题描述】:

你好,我是 swift 的初学者,我收到一个错误“无法将类型 'Character' 的值分配给类型 '[String]'”我该如何解决我的大脑现在迷失在这个代码博客 enter code here

导入 UIKit 导入 FirebaseFirestore 导入 FirebaseAuth 导入 Firebase 数据库 导入 FirebaseStorage

类PhoneViewController:UIViewController {

@IBOutlet weak var tableView: UITableView!

var phoneModelText = [String]()
var imeiAdressText = [String]()
var userNameText = [String]()
var idText = [String]()
var phoneNumberText = [String]()
var detailsText = [String]()
var dateText = [String]()
var priceText = [String]()
var adressText = [String]()


var selectedPhoneModelText = ""
var selectedimeiAdressText = ""
var selecteduserNameText = ""
var selectedidText = ""
var selectedphoneNumberText = ""
var selecteddetailsText = ""
var selecteddateText = ""
var selectedpriceText = ""
var selectedadressText = ""

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.prefersLargeTitles = true
    tableView.dataSource = self
    tableView.delegate = self
    getdata()
    
    
}

func makeAlert(titleInput: String, messageInput : String) {
    let alert = UIAlertController(title: titleInput, message: messageInput, preferredStyle: UIAlertController.Style.alert)
    let okButton = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
    alert.addAction(okButton)
    present(alert, animated: true, completion: nil)
}



func fetchBook(documentId: String) {
    let db = Firestore.firestore()
  let docRef = db.collection("Databases").document(documentId)
  docRef.getDocument { document, error in
    if let error = error as NSError? {
        self.makeAlert(titleInput: "alert", messageInput: "\(error.localizedDescription)")
    }
    else {
      if let document = document {
        let id = document.documentID
        let data = document.data()
        let phonemodel = data?["phoneName"] as? String ?? ""
        let imeiadress = data?["imeiNumberText"] as? Int ?? 0
        let username = data?["userNameText"] as? String ?? ""
        let idcard = data?["idCardtext"] as? Int ?? 0
        let phonenumber = data?["phoneNumberText"] as? Int ?? 0
        let adress = data?["adressNameText"] as? String ?? ""
        let details = data?["detailSectionText"] as? String ?? ""
        let date = data?["currentDateText"] as? String ?? ""
        let price = data?["priceValueText"] as? Int ?? 0
        let image = data?["imageurl"] as? String ?? ""
          
          

          DispatchQueue.main.async {
              self.selectedphoneNumberText = phonemodel
              
              
              
             self.phoneModelText.text = phonemodel
              self.imeiAdressText.text = String(imeiadress)
              self.userNameText.text = username
              self.idText.text = String(idcard)
              self.phoneNumberText.text = String(phonenumber)
              self.adressText.text = adress
              self.detailsText.text = details
              self.dateText.text = date
              self.priceText.text = String(price)

          }

      }
    }

  }
}

}

扩展 PhoneViewController : UITableViewDataSource,UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return phoneModelText.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = phoneModelText[indexPath.row]
    return cell
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // how i customizing there
    

  
           performSegue(withIdentifier: "toPhoneListView", sender: nil)
    }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toPhoneListView" {
        let destinationVC = segue.destination as! PhoneListViewController
        destinationVC.selectedPhoneModelText
        destinationVC.selectedimeiAdressText
        destinationVC.selecteduserNameText
        destinationVC.selectedidText
        destinationVC.selectedphoneNumberText
        destinationVC.selecteddetailsText
        destinationVC.selecteddateText
        destinationVC.selectedpriceText
        destinationVC.selectedadressText
       }
   }

}

【问题讨论】:

  • 为什么不创建一个包含所有属性的结构呢?至于错误,您将phonemodel 读取为字符串,然后尝试将其分配给phoneModelText.text,其中phoneModelText 被声明为字符串数组,这意味着它没有属性text。如果它只包含一个项目,为什么要将它声明为一个数组?
  • 你能让我理解吗,因为如果我看到我的大脑在工作,或者如果你给我类似的例子对我来说足够了谢谢
  • 究竟是什么,您的问题是关于表格视图,但如果您只从 firebase 加载一项,那么您不需要表格,对吧?而对于创建一个结构,这是非常基本的东西,struct Book { var phoneModelText: [String] var imeiAdressText = [String] ...//same for rest of the properties },当然你需要决定它是[String]还是只是String的类型。
  • 欢迎来到 SO。关于发布问题的几件事。 1) 代码格式非常重要——更漂亮的代码会得到更好的答案。 2)当您收到错误时,请务必指出导致错误的行,以便我们可以专注于问题

标签: ios swift firebase


【解决方案1】:

这就是你的 phoneModelText 的定义方式

var phoneModelText = [String]()

这表明phoneModelText是一个字符串数组,所以它看起来像这样

phoneModelText[0] = "Some String"
phoneModelText[1] = "Another string"

但稍后您尝试将字符串分配给该数组

self.phoneModelText.text = phonemodel

这不是数组的工作方式。如果你想将phoneModel 添加到数组中,那就是这个

self.phoneModelText.append(phoneModel) //assume phoneModel = "yet another string"

所以数组看起来像这样

phoneModelText[0] = "Some String"
phoneModelText[1] = "Another string"
phoneModelText[2] = "yet another string"

一般来说,我建议您命名您的 var,以便它们更能代表它们所包含的内容 - 而不是 phoneModelText,将其称为 phoneModelTextArray。这将减少混乱并使代码更具可读性。

至于解决方案,不清楚为什么会有一堆数组

var phoneModelText = [String]()
var imeiAdressText = [String]()
var userNameText = [String]()

但我建议改变所有这些。一种选择是定义一个具有属性的类,然后拥有一个类数组

class ContactClass {
   var id = ""
   var phoneText = ""
   var imeiAdressText = ""
   var userNameText = ""
}

然后是控制器中的类数组

var contactArray = [ContactClass]()

最后,当从 Firebase 读取数据时,实例化类,填充类属性并将类添加到数组中

else {
  if let document = document {
    let contact = ContactClass()
    
    contact.id = document.documentID
    contact.phoneText = data?["phoneName"] as? String ?? ""
    contact.imeiAdressText = data?["imeiNumberText"] as? Int ?? 0
    contact.userNameText = data?["userName"] as? String ?? ""
    
    self.contactArray.append(contact)

【讨论】:

  • 我还有一个问题 1 我将数据提取到 firebase 但是我如何传递其他 vc 你能帮我所有获取数据以在我点击时发送其他 vc evrey 单元格我需要我写的 didselectrowat 函数
  • @newcoder 听起来它最适合单独的问题!在 cmets 中回答这个问题并不难(代码在 cmets 中表现不佳,哈哈)。关于如何将数据传递到另一个视图控制器,这里还有很多其他问题——而且还有很多方法可以做到这一点。所以先做一些研究,然后用一些代码发布一个问题,我们会看看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2022-06-30
  • 2020-10-01
  • 2017-08-06
  • 1970-01-01
相关资源
最近更新 更多