2017-02-16 4 views
1

私はアプリを開発中で、配列内のデータを1つのView Controllerから別のView Controllerに送る必要があります。私はラベルから変数にStringを引っ張り、それを配列に追加しています。Swiftでの配列の受け渡し

私はその後、私は firstViewControllerから SecondViewController私secondViewControllerで

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

にデータを渡したいprepareForSegue機能を持って、私はtableViewために必要な機能がすべて揃っているが、私のtableViewが移入されません

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

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 
} 

何か不足していますか?

EDIT:secondViewControllerの配列が空であるため

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]にクラッシュを取得します。エラーが読み:

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"]

だから私は第1のアレイが充填され、第2のコントローラにデータを送信できないことを知っています。

+0

'tableView delegate'と' dataSource'を設定しましたか? – JuicyFruit

+0

配列を初期化しますか?使用する前にtimeArray = [String]()? – Greg

+0

両方とも有効です。私のtimeArrayは初期化されており、デリゲートとデータソースを持っています。 – jmur216

答えて

2

ビューコントローラ間の情報の引き渡しは、特にテーブルビューのうちの1つがテーブルビューである場合に、扱いにくいことがあります。イベントのタイミングはちょっと直感的ではありません。問題を避けるために、物事が特定の順序で読み込まれると想定してはいけません。あなたの特定のケースで

、それはtableView(tableView: UITableView, numberOfRowsInSection section: Int)可能ですセグエが起こる前に、(APIはこのが発生しませんので、我々はそのような場合を処理する必要が保証するものではありません)と呼ばれています。

修正:最初に、はありませんは、強制アンラップされたオプション(!演算子)を使用します。オプションがある場合は、使用する前に有効かどうかを確認する必要があります。これは、ある時点でデータが有効でない可能性がある「ヒント」です。

第二:このようなあなたのreloadData()

何かトリガするために、あなたのtimeArraydidSetを使用します。また

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

を、あなたのセグエ機能がより次のようにする必要があります:

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

destinationViewControllerはあなたが期待するものかもしれないし、そうでないかもしれません。

希望に役立ちます!

関連する問題