使用委托选择项目后 TableView 单元格未更新 Swift

TableView cell not updating after Selecting item using delegate Swift

我正在尝试为选择项实现委托方法。我在更新 tableView 单元格时感到困惑。因为我有多个部分,所以列表将用于不同的单元格。如何更新我的 tableview 单元格行或模型以便重新加载 tableView?

型号:


struct Unit : Codable {

    let sectionList : [SectionList]?

}
struct SectionList : Codable {

    let title : String?
    let items : [Item]?

}

struct Item : Codable {

    let actionType : Int?
    let actionUrl : String?
    let bgColor : String?
    let booleanValue : Bool?
    let textField : String?
    let textValue : String?
    let unitId : Int?
    let latitude : Double?
    let longitude : Double?
    let actionParamData: String?
    let actionTitle: String?
    let pickList: [SectionList]?
    let multiSelect: Bool?
    let selectedValue: [String]?
    let version: Int?
    let masterId: Int?
    let itemValue: String?
}



第一视图控制器:

var AppData: Unit?

func listFunc(tvc2: ViewController, didSelectList listValue: String) {
        print(listValue)
        let indexPathRow:Int = 0
        let indexPosition = IndexPath(row: indexPathRow, section: 0)
        tableView.reloadRows(at: [indexPosition], with: .none)
    }

SecondViewController:(包含列表)

protocol ListDelegate {
    func listFunc(tvc2: ViewController, didSelectList listValue: String)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let indexPath = tableView.indexPathForSelectedRow
        let currentCell = tableView.cellForRow(at: indexPath!)!
        //print(currentCell.textLabel?.text as Any)
        currentCell.accessoryType = .checkmark
        delegate?.listFunc(tvc2: self, didSelectList: currentCell.textLabel?.text ?? "")

        let vc = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
        self.navigationController?.pushViewController(vc, animated: true)


       }

您好,您对委托的工作方式有点困惑。我在这里修改你的代码。 假设你有两个视图控制器,即 FirstViewControllerSecondViewController 现在你想要的是发回数据或将数据从 SecondViewController 重新加载到 FirstViewController。所以你需要做的是在SecondViewController上创建一个Protocol并在FirstViewController中确认Protocol。以下是您的代码示例:

FirstViewController

class FirstViewController: UIViewControlller, ListDelegate {

   var selectedIndex: Int?
   var selectedSection: Int?

   //Click event for navigation from FirstViewController to SecondViewController
   @IBAction func BackButtonAction(_ sender: Any) {
       let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
       vc.delegate = self
       self.navigationController?.pushViewController(vc, animated: true)
   }

   func listFunc(listValue: String) {
       AppData?.sectionList?[selectedSection!].items?[selectedIndex!].textField = listValue
       tableView.reloadData()
   }

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       //Add these two line in your code
       selectedIndex = indexPath.row
       selectedSection = indexPath.section
   } 
}

SecondViewController

protocol ListDelegate {
    func listFunc(listValue: String)
}

class SecondViewController: UIViewControlller {
    var delegate: ListDelegate?

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let indexPath = tableView.indexPathForSelectedRow
        let currentCell = tableView.cellForRow(at: indexPath!)!
        //print(currentCell.textLabel?.text as Any)
        currentCell.accessoryType = .checkmark
        delegate?.listFunc(listValue: currentCell.textLabel?.text ?? "")
        self.navigationController?.popViewController(animated: true)
   }
}

Response.swift

//Change let to var for these
var textField : String?
var textValue : String?

以上代码可能会解决您的问题。请尝试此代码。 Happy To Help