ios - 使用 NIB 无法识别 UITableViewCell 中单击的单元格 UITableView

标签 ios iphone swift uitableview swift4

我正在编写一个具有包含多个单元格的仪表板的应用程序。其中一个单元格有问题,但答案是动态填充的,所以我决定使用 UITableView 来处理它。

我将 UITableViewCell 设置为内部 UITableView 的委托(delegate)和数据源,并进行了定义单元格和选定状态的配置。

extension SurveyTableViewCell: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        answers = model.getSurveyAnswers()

        return (answers?.count)!
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: InsideSurveyTableViewCell.identifier, for: indexPath) as! InsideSurveyTableViewCell

        cell.idAnswer.text = alphabetQuestion[indexPath.row]
        cell.answer.text = answers?[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100.0
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let cell = tableView.dequeueReusableCell(withIdentifier: InsideSurveyTableViewCell.identifier, for: indexPath) as! InsideSurveyTableViewCell

        cell.selectRow()
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = tableView.dequeueReusableCell(withIdentifier: InsideSurveyTableViewCell.identifier, for: indexPath) as! InsideSurveyTableViewCell

        cell.deselectRow()
    }
}

但无法识别内部 UITableViewCell 中单元格内的点击。在将用户答案发送到服务器后,我需要识别此点击。

我看到了一些解决方案,但使用的是 Storyboard。我在我的项目中只使用 Nib 。

但我仍然尝试使用我在 youtube 上看到的使用 Storyboard的方法。

在将使用内部 UITableView 的单元格上,我声明了一个函数来设置内部 tableView 的委托(delegate)和数据源,并给它一个标签。

extension SurveyTableViewCell {

    func setTableViewDataSourceDelegate<D:UITableViewDelegate & UITableViewDataSource>(_ dataSourceDelegate: D, forRow row: Int) {
        subTableView.dataSource = dataSourceDelegate
        subTableView.delegate = dataSourceDelegate

        subTableView.reloadData()
    }
}

比起管理外部 UITableView 的 viewController:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if tableView.tag == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: InsideSurveyTableViewCell.identifier) as! InsideSurveyTableViewCell

            cell.idAnswer.text = "A."
            cell.answer.text = "QUALQUER COISA"

            return cell
        }

        if retrivedCell is SurveyTableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: SurveyTableViewCell.identifier, for: indexPath) as! SurveyTableViewCell
            cell.delegate = self

            cell.setTableViewDataSourceDelegate(self, forRow: indexPath.row)

            cell.setPositionRow(row: indexPath.row - 1)
            cell.subTableView.rowHeight = UITableViewAutomaticDimension
            cell.subTableView.estimatedRowHeight = 50

            return cell
        }
 }


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView.tag == 1 {
            return 3
        }

        var numberOfCells: Int = 0

        if cellsToPresent != nil {
            numberOfCells = cellsToPresent!.count
        }

        return numberOfCells + 1
 }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView.tag == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: InsideSurveyTableViewCell.identifier) as! InsideSurveyTableViewCell

        cell.selectRow()
    }
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    if tableView.tag == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: InsideSurveyTableViewCell.identifier) as! InsideSurveyTableViewCell

        cell.deselectRow()
    }
}

selectRow 和 deselectRow 是改变内部 tableView 单元格标签的方法。

但还是没有成功。

如果我使用方法:

tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

应用程序中断提示我正在尝试使具有相同 indexPath 的不同单元格出队。

感谢您的帮助。

最佳答案

不要使用 让 cell = tableView.dequeueReusableCell(withIdentifier:)didSelectdidDeSelect 方法中。

使用

let cell = tableView.cellForRow(at: indexPath)

希望对您有所帮助。

关于ios - 使用 NIB 无法识别 UITableViewCell 中单击的单元格 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055144/

相关文章:

ios - 我需要一种方法来检索和记录通过 USB 连接的 iPhone 设备的 IMEI、序列号、ICCID 和 MEID

swift - 查询嵌套数据如何工作?我还能检索下一级的数据吗?

ios - 在 Swift 中使用 SRWebClient 上传多张图片

ios - 将 Image 设置为 Nil 会导致 ImageView 在堆栈 View 中消失

ios - 如何在 Flutter 插件中覆盖 iOS App 生命周期

iphone - 两个scrollView应该在任何一个scrollView中一起移动

iphone - 在单个 UIViewController 中有多个 UIView 的障碍

iphone - 如何为 UITableView 标题 View 设置动画

swift - NSUserDefaults 在其他场景调用

ios - 我已经尝试了所有方法,但无法将 Swift 文件导入到 Objective-C 文件