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

问题描述

我正在尝试获取在文本字段中输入的每个输入。我在我的另一个视图中使用它它有效,但在这里我在 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 (将#修改为@)