You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift结合Firebase开发遇类型赋值错误,如何自定义didSelectRowAt方法

问题修复方案

1. 报错Cannot assign value of type 'Character' to type '[String]'修复

错误原因

你定义的phoneModelText、imeiAdressText等变量均为[String]字符串数组类型,但是在fetchBook方法中错误地给数组调用了不存在的.text属性赋值,编译器误解析为字符串下标取值,返回Character类型,和数组类型不匹配触发报错。同时你存在变量名笔误:将selectedPhoneModelText错写为selectedphoneNumberText赋值。

修复代码

DispatchQueue.main.async {
    // 修正笔误
    self.selectedPhoneModelText = phonemodel
    // 如果是需要给数据源数组追加元素,删除.text用append方法
    self.phoneModelText.append(phonemodel)
    self.imeiAdressText.append(String(imeiadress))
    self.userNameText.append(username)
    self.idText.append(String(idcard))
    self.phoneNumberText.append(String(phonenumber))
    self.adressText.append(adress)
    self.detailsText.append(details)
    self.dateText.append(date)
    self.priceText.append(String(price))
    // 数据更新后刷新表格
    self.tableView.reloadData()
}

如果fetchBook是用于拉取单条详情数据,不需要修改数据源数组,直接删除所有给phoneModelText等数组赋值的代码即可。


2. UITableView选中行传值跳转实现

实现didSelectRowAt方法

选中行时取出对应下标位置的数据源,赋值给全局的选中临时变量:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // 取出当前行对应的数据
    selectedPhoneModelText = phoneModelText[indexPath.row]
    selectedimeiAdressText = imeiAdressText[indexPath.row]
    selecteduserNameText = userNameText[indexPath.row]
    selectedidText = idText[indexPath.row]
    selectedphoneNumberText = phoneNumberText[indexPath.row]
    selecteddetailsText = detailsText[indexPath.row]
    selecteddateText = dateText[indexPath.row]
    selectedpriceText = priceText[indexPath.row]
    selectedadressText = adressText[indexPath.row]
    // 触发跳转
    performSegue(withIdentifier: "toPhoneListView", sender: nil)
    // 优化体验:取消行选中状态
    tableView.deselectRow(at: indexPath, animated: true)
}

完善prepare方法传值

将临时变量的值赋值给目标控制器的对应属性:

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

额外优化建议

你当前用多个独立数组存储数据源,容易出现数组长度不一致导致的下标错位问题,建议定义单独的数据结构体统一管理:

// 定义数据模型
struct PhoneInfo {
    let model: String
    let imei: String
    let userName: String
    let idCard: String
    let phoneNumber: String
    let address: String
    let details: String
    let date: String
    let price: String
}
// 仅用一个数组作为数据源
var dataSource = [PhoneInfo]()

内容的提问来源于stack exchange,提问作者user12030685

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:27:10