swift 当对同一单元格 ID 使用两个不同的表视图时,indexPath.row 变得超出范围



由于我对同一视图控制器中的两个不同的表视图使用相同的 ViewCell,一旦我在一个表中的一个不同单元格内单击并返回到另一个表以进入另一个单元格,indexPath.row 正在增加,结果应用程序崩溃并为单元格提供"索引超出范围"?。TextLable.text = QRinstruction List[indexPath.row]

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    tableView.separatorColor = UIColor.black
    if (tableView == tableViewQR){
         cell = tableView.dequeueReusableCell(withIdentifier: "instructionCellId") as! InstructionsTableViewCell
         cell!.selectionStyle = UITableViewCell.SelectionStyle.none
        print("tableViewQR: count: (QRiconList.count)")
        print("tableViewQR: row: (indexPath.row)")
        print("tableViewQR: (QRiconList[indexPath.row])")
        cell?.iconInstructionImage.image = UIImage(named: QRiconList[indexPath.row])
         cell?.TextLable.text = QRinstructionList[indexPath.row]

        return cell!
    }else{
        cell = tableView.dequeueReusableCell(withIdentifier: "instructionCellId") as! InstructionsTableViewCell
        cell!.selectionStyle = UITableViewCell.SelectionStyle.none
        cell!.layer.borderColor = UIColor.black.cgColor
        cell?.TextLable.text = instructionList[indexPath.row]
        cell?.iconInstructionImage.image = UIImage(named: iconList[indexPath.row])
        //   print(cell?.IconNameVariable)
        if(cell?.TextLable.text == "MultipleCardInfo.Instructions".localized()){
        }
        return cell!
    }
}
您需要

numberOfRowsInSection中的每个表返回正确的计数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableView ==  tableViewQR ?  qRinstructionList.count :  instructionList.count
}

相关内容

最新更新