私はアプリを開発中で、配列内のデータを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のコントローラにデータを送信できないことを知っています。
'tableView delegate'と' dataSource'を設定しましたか? – JuicyFruit
配列を初期化しますか?使用する前にtimeArray = [String]()? – Greg
両方とも有効です。私のtimeArrayは初期化されており、デリゲートとデータソースを持っています。 – jmur216