swiftでシミュレーターを実行しても
Button.setTitle(ButtonArray[indexPath.row],forState: UIControlState.Normal)
の文に
EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)
↑がでてシミュレーターが強制終了されます。
どうすればでなくなりますか?
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
こちらの質問が他のユーザから「質問の範囲が広すぎる」という評価を受けています
わからない点を明確にし、調査したこと・試したことと共に記入していただくと、回答が得られやすくなります。
回答2件
0
ベストアンサー
indexPath.rowの値がButtonArrayの要素数より大きいんだと思います。
Consoleに、
fatal error: Array index out of range
と表示されていないでしょうか?
投稿2015/12/18 03:54
総合スコア16731
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/18 04:14
ででます!!
どのように直せばいいのですか?
2015/12/18 04:18
TableViewを使っているんだと思いますが、ButtonArrayの要素数をテーブルのセル数と同じ(もしくはそれ以上)にすればいいです。
具体的な回答が欲しいのであればコードを書いて下さい。
2015/12/18 04:23
import UIKit
class CollectionViewController: UICollectionViewController {
var Array = [String]()
var ButtonArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Array = ["平成27年","平成27年","平成26年","平成26年","平成25年","平成25年","平成24年","平成24年","平成23年","平成23年","平成22年","平成22年"]
ButtonArray = ["上期","下期","上期","下期","上期","下期","上期","下期","上期","下期"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Array.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
let label = cell.viewWithTag(1) as! UILabel
label.text = Array[indexPath.row]
let Button = cell.viewWithTag(2) as! UIButton
Button.setTitle(ButtonArray[indexPath.row], forState: UIControlState.Normal)
return cell
}
}
これなんですけど…
2015/12/18 05:19 編集
Arrayに比べてButtonArrayの要素が少ないだけです。
ButtonArrayに "上期","下期" を追加して下さい。
2015/12/18 04:56
神かよ!!
2015/12/18 04:58
できました…
ありがとうございました!!
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。