Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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
Arrays 将数据复制到tableview_Arrays_Swift_Uitableview_Segue_Cell - Fatal编程技术网

Arrays 将数据复制到tableview

Arrays 将数据复制到tableview,arrays,swift,uitableview,segue,cell,Arrays,Swift,Uitableview,Segue,Cell,在我的第一个vc(ViewController)中,我有一个按钮,带有动作performsgue。在override funcprepare(对于segue:)中,我想将数据发送到另一个视图控制器 这是我的代码: if segue.identifier == "nextVC" { let data1 = ["TITLE", "TITLE2"] let data2 = ["DESCRIPTION", "DESCRIPTION 2", "DESCRIPTION 3"]

在我的第一个vc(ViewController)中,我有一个按钮,带有动作
performsgue
。在override func
prepare(对于segue:)
中,我想将数据发送到另一个视图控制器

这是我的代码:

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)中,我有变量
cellString1
cellString2
array
,如下所示:

var array: [String] = []
var cellString1: [String] = []
var cellString2: [String] = []
["0", "1", "1", "0", "1"]
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
在数组中,我发送到第一个vc中tableView单元格的第二个vc id,如下所示:

var array: [String] = []
var cellString1: [String] = []
var cellString2: [String] = []
["0", "1", "1", "0", "1"]
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
在第二个vc中,我也有tableView,其代码(在tableView(cellForRowAt:)中)如下所示:

var array: [String] = []
var cellString1: [String] = []
var cellString2: [String] = []
["0", "1", "1", "0", "1"]
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),然后单元格的标签从
cellString1
cellString2
中获取值,并显示此标签的文本。当我删除单元格textLabel配置时,单元格背景的颜色是正确的(
黑色、红色、红色、黑色、红色)
但是在完整代码中,我有错误
(索引超出范围)
单元格.textLabel?行中。text=cellString1[indexPath.row]
。 是否可以修复此问题?

这应该是:

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

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

您有2个标题、3个说明和5个颜色标志。如果使用多个数组作为数据源,则必须确保项目数始终相同

例如,如果在
numberOfRows
中返回
array.count
,则调用
cellForRow
5次。但是在
cellString1
中的
indexPath.row>1
cellString2
中的
indexPath.row>2
处没有项目。这会导致超出范围异常

基本上不鼓励您使用多个阵列作为数据源。不要那样做。

使用自定义结构,而不是“0”和“1”字符串使用
Bool

struct Item {
   var title : String
   var description : String
   var colorFlag : Bool
}

然后使用适当的数据创建
Item
实例,并传递one数组。

这是预期的,因为3个数组具有不同的计数,并且确保
cellString1
的indexPath将超过,因为它包含2个项,第二个为“0”数组的个数在索引3中,超过了
cellString1的个数

我正在尝试在编写时修复我的问题,但仍然无法工作。我能把我的全部密码寄给你检查一下吗?