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

我所做的是:

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

    ios - 如何在动态UITableView xcode项目中更改单元格中的数据?-LMLPHP
  • 我将文本字段等,操作和引用附加到这些类。
  • 这是我的cellAtRow函数,我想我会以某种方式更改它?
    还是从这里引用 class ?
    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
        }
    
  • 最佳答案

    您必须将单元格强制转换为它们所属的类。在代码块的第二行,您可以看到一个示例。

    if (sectionsData[indexPath[0]] == "header") {
        let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as! HeaderTableViewCell
    
        cell.titleLbl.text = "Title"
        cell.delegate = self // To receive actions back
    
        return cell
    }
    
    . . . // More of the same
    
    // default return
    

    要将 call 发送回您的主视图控制器,您可以将协议添加到单元格中,如下所示:
    protocol HeadTableViewCellProcol{
        func bttnPressed()
    }
    
    class HeadTableViewCell: UITableViewCell{
    
        var delegate: HeadTableViewCellProcol?
    
        @IBAction func bttnPressedInCell(){
            delegate?.bttnPressed()
        }
    }
    

    此协议的此协议类似于您必须为UITableView实现的协议。您还必须在主VC中实现这些协议。

    关于ios - 如何在动态UITableView xcode项目中更改单元格中的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53534508/

    10-14 23:23
    查看更多