首页 > 解决方案 > 如何在动态 UITableView xcode 项目中更改单元格中的数据?

问题描述

我有一个具有不同单元格的动态原型表视图,我正在将单元格添加到表视图中,并且我想更改它们的内容。我找到的所有教程都是针对只有一种类型的单元格的表格视图,但我有 8 种不同的类型。我将如何更改它们的内容(即文本字段等)以及如何将它们的操作返回到主 tableview 控制器以执行一些业务逻辑?(即按下按钮等)

我所做的是:

  1. 我为每种单元格类型创建了一个服装类,并将它们连接到 customClass、类字段下。

    在此处输入图像描述

  2. 我附加了这些类的文本字段等、操作和引用。

  3. 这是我的 cellAtRow 函数,我假设我会以某种方式在此函数中更改它?或从这里参考课程?

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        print ("indexPath: ", indexPath)
        print ("indexPath: ", indexPath[0])
        print ("-------")
    
        if (sectionsData[indexPath[0]] == "header") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "description") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "headerInfoCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "diagnoses") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "diagnosisCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "perscription") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "perscriptionCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "notes") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "notesCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "addFaxHeadline") {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "addFaxCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "addFax") {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "emailNameCell", for: indexPath)
    
            return cell
    
    
        } else if (sectionsData[indexPath[0]] == "addEmailHeadline") {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "addEmailCell", for: indexPath)
    
            return cell
    
    
        } else if (sectionsData[indexPath[0]] == "addEmails") {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "emailNameCell", for: indexPath)
    
            return cell
    
    
        } else if (sectionsData[indexPath[0]] == "givePermissionHeadline") {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "permissionCell", for: indexPath)
    
            return cell
    
        } else if (sectionsData[indexPath[0]] == "select answer") {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "selectAnswerCell", for: indexPath)
    
            return cell
        }
    

标签: iosswiftuitableview

解决方案


你需要使用

let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as! HeaderTableViewCell

cell.yourTextField.text例如打电话


推荐阅读