【问题标题】:Use arrays from tableView if statement in another viewcontrollers在另一个视图控制器中使用 tableView if 语句中的数组
【发布时间】:2020-04-08 21:44:55
【问题描述】:

我需要根据表视图中的 if 结果创建两个“帮助”数组。 我的主要问题是:如何在另一个视图控制器中使用这个数组?

等:因为当我想通过 TableViewController().helpArrayOne 在另一个控制器中使用一个数组时,它在 tableViewController 中为空/或不存在,但在 tableViewController 中打印后我得到了值,谢谢。

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

 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell

 let listOfStudents = allUrl.reduce(into: [String:String]()){$0[$1.id] =  $1.stringValue}

        for key in listOfStudents{

            // Compare data

           if  cell.textLabel?.text == key.key {

           cell.imageView!.image = UIImage(named:"false_icon")

           var helpArrayOne:[String] = []

           helpArrayOne.append(all[indexPath.row].id)

           var helpArrayTwo:[String] = []

           helpArrayTwo.append(all[indexPath.row].stringValue)

           break

        } else {

            cell.imageView!.image = UIImage(named:"true_icon")

                }
        }
        print(helpArrayOne,helpArrayTwo)
        return cell
    }

【问题讨论】:

    标签: arrays swift if-statement tableview


    【解决方案1】:

    记住范围;将数组声明为可选的实例变量。然后,您将能够从当前视图控制器范围内的其他任何地方将它们传递给另一个视图控制器。

    只需确保在将值分配给它们之后传递它们即可。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { [weak self] in
    
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
    
     let listOfStudents = allUrl.reduce(into: [String:String]()){$0[$1.id] =  $1.stringValue}
    
            for key in listOfStudents{
    
                // Compare data
    
               if  cell.textLabel?.text == key.key {
    
               cell.imageView!.image = UIImage(named:"false_icon")
    
    
    
               self?.helpArrayOne.append(all[indexPath.row].id)
    
    
    
               self?.helpArrayTwo.append(all[indexPath.row].stringValue)
    
               break
    
            } else {
    
                cell.imageView!.image = UIImage(named:"true_icon")
    
                    }
            }
            print(helpArrayOne,helpArrayTwo)
            return cell
        }
    

    【讨论】:

    • sry,我尝试编译并且没有错误,但是当我在此视图控制器上进行选项卡时,我在以下行出现错误:self?.helpArrayOne.append(all[indexPath.row]. id)
    • 不能对 ViewController.Replace 类型的非可选值使用可选链?与“。ViewController 类型的值没有成员 helpArrayOne
    • 有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多