我正在编写一个具有包含多个单元格的仪表板的应用程序。其中一个单元格有问题,但答案是动态填充的,所以我决定使用 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:)
在 didSelect
或 didDeSelect
方法中。
使用
let cell = tableView.cellForRow(at: indexPath)
希望对您有所帮助。
关于ios - 使用 NIB 无法识别 UITableViewCell 中单击的单元格 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055144/