微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

表视图中的文本字段委托错误:索引超出范围迅速

如何解决表视图中的文本字段委托错误:索引超出范围迅速

我正在尝试获取在文本字段中输入的每个输入。我在我的另一个视图中使用它它有效,但在这里我在 1 个部分中有不同的情况(应该更容易)但我不知道为什么,索引超出范围。代码如下:

import UIKit

struct SpecData {
var server: Int
var core: Int
var userCAL: Int
var deviceCAL: Int
}

var arraySpecDataInputed = [SpecData]()

extension SpecVC: UITableViewDelegate,UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return 4
}

func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 1:
        return 4
    default:
        return 1
    }
}

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let inputCell = tableView.dequeueReusableCell(withIdentifier: LicenseInputTVCell.cellIdentifier,for: indexPath) as! LicenseInputTVCell

switch indexPath.section {
case 1:
        inputCell.labelSpec.text = specserver[indexPath.row].spec
        inputCell.textfieldInput.delegate = self
        inputCell.textfieldInput.addTarget(self,action: #selector(textFieldDidChange),for: .editingChanged)
        inputCell.textfieldInput.tag = indexPath.row
        
        return inputCell
default:
        return inputCell
     }
   }
}

extension SpecVC: UITextFieldDelegate {
     @objc func textFieldDidChange(_ sender: UITextField) {
         let rowIndex = sender.tag
         let indexPath = IndexPath(row: rowIndex,section: 1)
         let cell = tableView.cellForRow(at: indexPath) as! LicenseInputTVCell
         if rowIndex == 0 {
             arraySpecDataInputed[rowIndex].server = Int(cell.textfieldInput.text!) ?? 0 // Fatal error: Index out of range
         }
     }
}

当我尝试开始在 textField 中输入 1 位数字时,应用程序出错,索引超出范围。我试图在 [rowIndex] 中输入一些不同的数字,但它不起作用。 谁能告诉我我在这里做错了什么? [rowIndex] 应该填什么?

提前致谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。