【问题标题】:Passing Arrays in Swift在 Swift 中传递数组
【发布时间】:2017-02-16 17:13:55
【问题描述】:

我正在开发一个应用程序,我需要将数组中的数据从一个视图控制器发送到另一个视图控制器。我正在将一个字符串从标签中提取到一个变量中并将其附加到数组中。

var time:String = timeLabel.text!
timeArray.append(time)
print("add data")

然后我有一个prepareForSegue 函数,我想将数据从firstViewController 传递到SecondViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    let nvc = segue.destinationViewController as! SecondViewController
    nvc.timeArray2 = timeArray
}

在我的 secondViewController 中,我拥有 tableView 的所有必要功能,但我的 tableView 永远不会填充任何数据,因为 timeArray2 是空的,并且会导致崩溃或空的 tableView .

override func viewWillAppear(animated: Bool)
{
    scrambleTimeTableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return timeArray2.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = scrambleTimeTableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
    
    cell.textLabel?.text = timeArray2[indexPath.row]
    cell.textLabel?.font = UIFont(name: "Arial", size: 18)
    cell.textLabel?.textColor = UIColor.blueColor()
    
    print("Populate tableview")
    
    return cell
}

我缺少什么吗?

编辑:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    
    let cell = scrambleTimeTableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
    
    cell.textLabel?.text = timeArray2[indexPath.row]
    cell.textLabel?.font = UIFont(name: "Arial", size: 18)
    cell.textLabel?.textColor = UIColor.blueColor()
    
    print("Populate tableview")
    
    return cell
}

我在cell.textLabel?.text = timeArray2[indexPath.row] 上发生崩溃,因为 secondViewController 中的数组为空。错误显示:

线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

输出显示:

[]

致命错误:索引超出范围

(lldb)

我让它打印数组,如果它是满/空的,以及数组是否填充到 firstViewController,看起来像这样:

添加数据

[“01.12”]

添加数据

["01.12", "01.48"]

所以我知道第一个数组正在填充,它无法向第二个控制器发送任何数据。

【问题讨论】:

  • 你设置tableView delegatedataSource了吗?
  • 你在使用之前初始化数组:timeArray = [String]() 吗?
  • 两者都是,我的 timeArray 已初始化,我有委托和数据源
  • 尝试将 scrambleTimeTableView.reloadData() 移动到 viewDidAppeare 并添加 print(timeArray2) 以查看是否有数据。
  • 它从不打印数据,所以我假设它从不加载函数,但是很难测试任何东西,因为数组仍然加载为空并导致它崩溃

标签: ios arrays swift uitableview


【解决方案1】:

在视图控制器之间传递信息可能很棘手,尤其是当其中一个是 tableview 时。事件发生的时间有点违反直觉。为避免出现问题,您不应假设事物以任何特定顺序加载。

在您的具体情况下,tableView(tableView: UITableView, numberOfRowsInSection section: Int) 可能在 segue 发生之前被调用(API 不保证这种情况不会发生,因此我们需要处理这种情况)。

修复:首先,从不使用强制解包选项(! 运算符)。选项的存在正是出于这个原因:如果某些东西是可选的,您需要在使用它之前检查它是否有效——这是一个“提示”,即数据可能在某个时间点无效。

第二:在您的timeArray 中使用didSet 来触发您的reloadData()

类似这样的:

var timeArray: [MyTimeType] = [] {
   didSet {
      tableView.reloadData()
   }
}

另外,你的 segue 函数应该更像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if let nvc = segue.destinationViewController as? SecondViewController {
        nvc.timeArray2 = timeArray
    }
}

destinationViewController 可能是也可能不是您所期望的——您应该经常检查。

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2014-08-06
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    相关资源
    最近更新 更多