如何在SecondVC中连续显示tableViewCell的详细文本



我有两个vc。第一个包含三个元素和三个标题。我想在SecondVC中展示每一行的详细版本。我已经写了下面的代码,但所有三行在SecondVc(头和文本)显示了FirstVc的第一行文本。我如何在第二个VC中使每一行都与第一个VC完全相同。下面是我的代码:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return items[row]?.title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell" , for: indexPath)
cell.textLabel?.text = items[indexPath.row]?.content 
cell.textLabel?.font = UIFont.italicSystemFont(ofSize: 12.0)
cell.textLabel?.textColor = .black
cell.textLabel?.alpha = 0.5 
return cell
}

提前谢谢你

这是我如何试图在控制器之间传递数据:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

let category = result?.data[indexPath.section]

let vc = ListViewController(items: result!.data) 
vc.title = category?.title
navigationController?.pushViewController(vc, animated: true)
}

didSelectRowAt中,您在category的基础上设置标题,但是您传递了整个result!.data而没有告诉它选择了哪一行。当然,标题大概是正确的,但它无法知道应该显示哪一行的详细信息。

您可以通过添加一个参数来解决这个问题,该参数指示选择了哪一行。但我建议重构这个,而不是传递整个数组result!.data,而只是选定的行(例如,您的category变量的内容)。因此,更改第二个视图控制器的init方法,不要期望整个数组,而是单行,然后提供category

最新更新