Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在dynamic UITableView xcode项目中更改单元格中的数据?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在dynamic UITableView xcode项目中更改单元格中的数据?

Ios 如何在dynamic UITableView xcode项目中更改单元格中的数据?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个动态原型表视图,它有不同的单元格,我将单元格添加到表视图中,我想更改它们的内容。我找到的所有教程都是针对只有一种单元格类型的tableview的,但我有8种不同的类型。我将如何更改它们的内容(例如,文本字段等),以及如何将它们的操作返回到主tableview控制器以执行一些业务逻辑?按下按钮等 我所做的是: 我为每个单元类型创建了一个costment类,并将它们连接到customClass、class字段下 我附加了这些类的文本字段等、操作和引用 这是我的cellAtRow函数我想我会在

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

我所做的是:

我为每个单元类型创建了一个costment类,并将它们连接到customClass、class字段下

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

这是我的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
    }
你需要使用

let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as! HeaderTableViewCell
例如,要调用cell.yourTextField.text,您需要使用

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

例如,要调用cell.yourTextField.text,您必须将单元格强制转换为它们所属的类。在代码块的第二行,您可以看到这样一个示例

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
要将呼叫发送回主视图控制器,您可以向单元格添加协议,如下所示:

protocol HeadTableViewCellProcol{
    func bttnPressed()
}

class HeadTableViewCell: UITableViewCell{

    var delegate: HeadTableViewCellProcol?

    @IBAction func bttnPressedInCell(){
        delegate?.bttnPressed()
    }
}

这些协议就像您必须为UITableView实现的协议一样。您还必须在主VC中实现这些协议。

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

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
要将呼叫发送回主视图控制器,您可以向单元格添加协议,如下所示:

protocol HeadTableViewCellProcol{
    func bttnPressed()
}

class HeadTableViewCell: UITableViewCell{

    var delegate: HeadTableViewCellProcol?

    @IBAction func bttnPressedInCell(){
        delegate?.bttnPressed()
    }
}
这些协议就像您必须为UITableView实现的协议一样。您还必须在主VC中实现这些协议。

您需要将UITableViewCell强制转换为动态单元格类。您可以尝试以下操作:

guard let cell = tableView. dequeueReusableCell(withIdentifier: "perscription", for: indexPath) as? PerscriptionTableViewCell else { return UITableViewCell() }

cell.setupCell() //You have access to cell's public funcs and vars now
return cell
使用可选的展开,您可以确保您的应用程序可能不会受到类型转换崩溃的影响

您需要将UITableViewCell强制转换为动态单元格类。您可以尝试以下操作:

guard let cell = tableView. dequeueReusableCell(withIdentifier: "perscription", for: indexPath) as? PerscriptionTableViewCell else { return UITableViewCell() }

cell.setupCell() //You have access to cell's public funcs and vars now
return cell
使用可选的展开,您可以确保您的应用程序可能不会受到类型转换崩溃的影响

正如前面所说,dequeueReusableCell的返回类型是UITableViewCell

苹果文档 返回值:具有关联标识符的UITableViewCell对象,如果可重用单元队列中不存在此类对象,则返回nil

自定义单元格类应从UITableViewCell继承,为了能够使用自定义单元格的实例,需要将dequeReusableCell返回的UITableViewCell强制转换为所需的自定义单元格类型

让cell=tableView.dequeueReusableCellwithIdentifier:customCellIdentifierCell,for:indexPath as!您的切割表视图单元格

对于定制,每个单元负责自己的配置。您应该有一个可以使用协议或从超类继承的函数,在CellForRowatineXpath中,在强制转换后,调用setup函数

customCell.setup//如果需要,您可以添加一些参数

,如所述,dequeueReusableCell的返回类型是UITableViewCell

苹果文档 返回值:具有关联标识符的UITableViewCell对象,如果可重用单元队列中不存在此类对象,则返回nil

自定义单元格类应从UITableViewCell继承,为了能够使用自定义单元格的实例,需要将dequeReusableCell返回的UITableViewCell强制转换为所需的自定义单元格类型

让cell=tableView.dequeueReusableCellwithIdentifier:customCellIdentifierCell,for:indexPath as!您的切割表视图单元格

对于定制,每个单元负责自己的配置。您应该有一个可以使用协议或从超类继承的函数,在CellForRowatineXpath中,在强制转换后,调用setup函数


customCell.setup//如果需要,您可以添加一些参数

请不要有条件地向下转换单元格。用力打开它们。很安全。除非进行了设计,否则代码不能崩溃mistake@vadian您的应用程序存在一些功能问题或无法按预期工作是可以的,但您的应用程序崩溃是不可接受的。reasean并不重要,您必须始终防止潜在的崩溃点。再一次,如果在设计时一切都正确连接,代码就不会崩溃。没有潜在的崩溃点是的,它可以。考虑您的朋友有一个包含自定义UITable ViWeCell的NIB文件,并用不同的单元格标识符将其注册到TabelVIEW。或者,您可以想象您的队友将类型单元格更改为另一个自定义UITableViewCell。你不能说你没有犯错误。通常,您必须避免在代码中强制展开。无意冒犯,但运行时检查(如条件降级)作为疏忽不测试UI的借口是荒谬的。做你的家庭作业。是的,代码不会崩溃,但表视图会崩溃
oesn也不起作用,而且很有可能会因为缺少插座而出现编译错误。只有少数情况下强制展开是有用的。这是其中之一。请不要有条件地向下投射单元格。用力打开它们。很安全。除非进行了设计,否则代码不能崩溃mistake@vadian您的应用程序存在一些功能问题或无法按预期工作是可以的,但您的应用程序崩溃是不可接受的。reasean并不重要,您必须始终防止潜在的崩溃点。再一次,如果在设计时一切都正确连接,代码就不会崩溃。没有潜在的崩溃点是的,它可以。考虑您的朋友有一个包含自定义UITable ViWeCell的NIB文件,并用不同的单元格标识符将其注册到TabelVIEW。或者,您可以想象您的队友将类型单元格更改为另一个自定义UITableViewCell。你不能说你没有犯错误。通常,您必须避免在代码中强制展开。无意冒犯,但运行时检查(如条件降级)作为疏忽不测试UI的借口是荒谬的。做你的家庭作业。是的,代码没有崩溃,但是表视图也不工作,而且很可能您会得到关于缺少插座的编译错误。只有少数情况下强制展开是有用的。这是其中之一。一切都很好,但在tableview控制器和view控制器中,协议无法读取itself@ChiefMadog什么意思?你有一个tableviewcontroller吗?您是否在询问如何实现协议以实际接收回拨?一切都很好,但在tableview控制器和view控制器中,协议不会被读取itself@ChiefMadog什么意思?你有一个tableviewcontroller吗?您是否在询问如何实现您的协议以实际接收回拨?