【问题标题】:TableView cell not updating after Selecting item using delegate Swift使用委托 Swift 选择项目后 TableView 单元格未更新
【发布时间】:2019-12-02 08:17:44
【问题描述】:

我正在尝试为选择项实现委托方法。我在更新 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?
}



FirstViewController:

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)


       }

【问题讨论】:

  • 你在哪里将第一个视图控制器设置为第二个的代理?为什么总是重新加载第 0 行?
  • @Paulw11 在secondViewController 中设置委托。 var delegate: ListDelegate?
  • @Paulw11 实际的简单扫描仪是我必须从secondViewController 中选择任何项目并希望在FirstViewController 中设置。一旦项目选择它刷新单元格。
  • 这就是您对委托进行处理的地方,但您需要将您的第一个视图控制器的实例设置为该属性的某个地方
  • @Paulw11 我真的很陌生,对如何设置感到困惑?我可以将我的github 链接分享给您吗?

标签: ios swift uitableview appdelegate


【解决方案1】:

嗨,您对委托的工作方式有点困惑。在这里,我正在修改您的代码。 假设您有两个视图控制器,即FirstViewControllerSecondViewController,现在您想要的是从SecondViewControllerFirstViewController 发送回数据或重新加载数据。所以你需要做的是在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
   } 
}

第二视图控制器

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

【讨论】:

  • 谢谢 :) 现在可以正常工作了。还有一件事要知道如何为这些更改重新加载表格视图/模型?
  • @NewBieMobile listFunc FirstViewController 中的方法将有助于重新加载您的 tableView。
  • 是的,它不会重新加载 tableView 数据。选择时存在相同的数据。
  • 好的,我明白了。要在 tableViewCell 中反映您的新值,您需要将其保存在另一个变量中,然后在 cellForRowAt: index 方法中分配它,或者您可以直接在 listFunc 方法中更新您的数据模型。
  • 谢谢,这是链接。 github.com/MasamMahmood/SqliteDataList
猜你喜欢
  • 2012-04-06
  • 2015-04-20
  • 1970-01-01
  • 2020-03-26
  • 2022-07-05
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2021-05-08
相关资源
最近更新 更多