ios - 在 Swift 中传递数组

标签 ios arrays swift uitableview

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

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 中的数组是空的。错误如下:

Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

输出显示:

empty

[]

fatal error: Index out of range

(lldb)

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

add data

["01.12"]

add data

["01.12", "01.48"]

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

最佳答案

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

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

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

第二:在你的 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 可能是也可能不是您所期望的——您应该经常检查。

希望对您有所帮助!

关于ios - 在 Swift 中传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42280545/

相关文章:

ios - 从 videoView、Twilio 视频中清除一帧

ios - WKInterfaceTable 在添加行时显示 Storyboard 数据

javascript - 连接数组 [a,c,e] 和 [b,d,f] 使其等于 [ab,cd,ef] - JavaScript

java - 使用自动名称创建数组?

ios - inputAccessoryView 的 UIToolbar 在 iOS 中旋转时变黑

ios - 如何使用 UIAppearance 设置 ViewController 的背景颜色

ios - 如何根据在 UITableViewCell 中点击的单元格进行判断?

ios - 无法在 iOS 上的 Swift 中将 base64 字符串发送到服务器

arrays - 如何在 Swift 中拆分没有分隔符的字符串

swift - 如何创建一个通用的完成闭包?