質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

4200閲覧

[swift]pickerの初期値が設定できない

SATOU_UKI

総合スコア29

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/02/04 04:08

cellに配置したpickerの初期の値を設定したいのですが、できません。
下記のコードで、何が間違っているのでしょうか?

swift3

1コード

import UIKit
//xlibで作成したcell
class List_Act: UITableViewCell{

@IBOutlet var ActImage: UIImageView! @IBOutlet var ActTitle: UILabel! @IBOutlet var ActPicker: UIPickerView! override func awakeFromNib() { super.awakeFromNib() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) }

}

swift3

1コード

class ActListVC: UIViewController, UITableViewDelegate, UITableViewDataSource ,UIPickerViewDelegate,UIPickerViewDataSource{

private var MyUIPicker:UIPickerView! var PickerViewCount:[Int] = [] var PickerViewCountList:[[String]] = [["a"."b"],["c","d"],,["e","f"]] var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() MyUIPicker = MyUIPickerView() MyUIPicker.isUserInteractionEnabled = true MyUIPicker.frame = CGRect(x: 0, y: 0, width: 100, height: 100) let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: displayWidth, height: displayHeight) // Cellの登録. let xib = UINib(nibName: "List_Act", bundle: nil) myTableView.register(xib,forCellReuseIdentifier:"Cell_Act") // DataSourceの設定. myTableView.dataSource = self // Delegateを設定. myTableView.delegate = self // TableViewをViewに追加する. self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } //Cellに値を設定する func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let tap = UITapGestureRecognizer(target: self, action: #selector(PickerTap(sender:))) let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_Act") as! List_Act cell.ActPicker.delegate = self cell.ActPicker.dataSource = self cell.ActPicker.tag = indexPath.row cell.ActPicker = MyUIPicker //pickerの初期値を設定したつもりが、反映されない cell.ActPicker.selectRow(1, inComponent: 0, animated: false) return cell }

// pickerに表示する列数を返すデータソースメソッド.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

// pickerに表示する行数を返すデータソースメソッド
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent
component: Int) -> Int {
return 2
}

//pickerに値を返すデリゲートメソッド. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return PickerViewCountList[Picker¥View.tag][row] } //pickerが選択された際に呼ばれるデリゲートメソッド. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {}

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{} 内で、

cell.ActPicker = MyUIPicker
と定義していたことが問題でした。

投稿2017/02/04 08:54

SATOU_UKI

総合スコア29

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問