gpt4 book ai didi

arrays - 获取数据到 TableView

转载 作者:行者123 更新时间:2023-11-30 11:21:36 25 4
gpt4 key购买 nike

在我的第一个 vc (ViewController) 中,我有带有操作 performSegue 的按钮。在 override func prepare(for segue:) 中,我想将数据发送到另一个 View Controller 。

这是我的代码:

if segue.identifier == "nextVC" {
let data1 = ["TITLE", "TITLE2"]
let data2 = ["DESCRIPTION", "DESCRIPTION 2", "DESCRIPTION 3"]
let destination = segue.destination as! DestinationController
destination.cellString1 = data1
destination.cellString2 = data2
destination.array = array

在我的第二个 vc (DestinationController) 中,我有变量 cellString1cellString2array,如下所示:

var array: [String] = []
var cellString1: [String] = []
var cellString2: [String] = []

在数组中,我将第一个 vc 中的 tableView 单元格的第二个 vc id 发送到,如下所示:

["0", "1", "1", "0", "1"]

在第二个 vc 中,我也有 tableView 和代码(在 tableview(cellForRowAt:) 中),如下所示:

if array[indexPath.row] == "0" {
cell.textLabel?.text = cellString1[indexPath.row]
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 16)
cell.backgroundColor = UIColor.black
return cell
} else if array[indexPath.row] == "1" {
cell2.textLabel?.text = cellString2[indexPath.row]
cell2.textLabel?.textColor = UIColor.gray
cell2.textLabel?.font = UIFont.systemFont(ofSize: 12)
cell2.backgroundColor = UIColor.red
return cell2

我想检测单元格中的数组是否有值(0或1),然后单元格的标签从cellString1cellString2获取值并显示该标签的文本。当我删除单元格 textLabel 配置时,单元格背景的颜色是正确的(黑色,红色,红色,黑色,红色),但是在完整代码中我有错误(索引超出范围)cell.textLabel?.text = cellString1[indexPath.row] 行中。可以解决这个问题吗?

最佳答案

这应该是:

    destination.cellString1 = data1
destination.cellString2 = data2
destination.array = array
let destination = segue.destination as! DestinationController

您应该在destination.cellString1 = data1destination.cellString2 = data2destination.array = array之后执行segue。否则这些属性将不会被分配。

关于arrays - 获取数据到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51215830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com