Created
April 13, 2018 07:41
-
-
Save uruly/eeccb3a94320efe5c16f4801d2f6ee97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PickerTableViewCell: UITableViewCell { | |
public var rightLabel:UILabel! | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
} | |
override func setSelected(_ selected: Bool, animated: Bool) { | |
super.setSelected(selected, animated: animated) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override init(style: UITableViewCellStyle, reuseIdentifier: String!) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
setup() | |
} | |
func setup() { | |
rightLabel = UILabel() | |
rightLabel.frame = CGRect(x:self.frame.width / 2,y:0,width:self.frame.width / 2,height:self.frame.height) | |
rightLabel.textAlignment = .center | |
self.accessoryView = rightLabel | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PickerTableViewController: UIViewController{ | |
//テーブルビュー | |
private var tableView:UITableView! | |
//ピッカービュー | |
private var pickerView:UIPickerView! | |
private let pickerViewHeight:CGFloat = 160 | |
//pickerViewの上にのせるtoolbar | |
private var pickerToolbar:UIToolbar! | |
private let toolbarHeight:CGFloat = 40.0 | |
//テーブルビューのテキストラベル | |
private let textArray = ["性別","好きな動物","好きなスポーツ"] | |
//ピッカービューの選択肢 | |
private let genderArray = ["男","女"] | |
private let animalArray = ["猫","犬","ライオン","カバ","キリン","ゾウ"] | |
private let sportsArray = ["野球","サッカー","バレーボール","テニス","水泳"] | |
//ピッカービューに渡すIndexPath | |
private var pickerIndexPath:IndexPath! | |
//現在の値 | |
private var currentGender:String! | |
private var currentAnimal:String! | |
private var currentSports:String! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let width = self.view.frame.width | |
let height = self.view.frame.height | |
//初期値 | |
currentGender = genderArray[0] | |
currentAnimal = animalArray[0] | |
currentSports = sportsArray[0] | |
//tableView | |
tableView = UITableView(frame: CGRect(x:0,y:50,width:width,height:height - 50)) | |
tableView.delegate = self | |
tableView.dataSource = self | |
tableView.register(PickerTableViewCell.self, forCellReuseIdentifier: "cell") | |
self.view.addSubview(tableView) | |
//pickerView | |
pickerView = UIPickerView(frame:CGRect(x:0,y:height + toolbarHeight, | |
width:width,height:pickerViewHeight)) | |
pickerView.dataSource = self | |
pickerView.delegate = self | |
pickerView.backgroundColor = UIColor.gray | |
self.view.addSubview(pickerView) | |
//pickerToolbar | |
pickerToolbar = UIToolbar(frame:CGRect(x:0,y:height,width:width,height:toolbarHeight)) | |
let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) | |
let doneBtn = UIBarButtonItem(title: "完了", style: .plain, target: self, action: #selector(self.doneTapped)) | |
pickerToolbar.items = [flexible,doneBtn] | |
self.view.addSubview(pickerToolbar) | |
} | |
@objc func doneTapped(){ | |
UIView.animate(withDuration: 0.2){ | |
self.pickerToolbar.frame.origin.y = self.view.frame.height | |
self.pickerView.frame.origin.y = self.view.frame.height + self.toolbarHeight | |
self.tableView.contentOffset.y = 0 | |
} | |
self.tableView.deselectRow(at: pickerIndexPath, animated: true) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
extension PickerTableViewController: UITableViewDelegate { | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
//ピッカービューとセルがかぶる時はスクロール | |
let cell = tableView.cellForRow(at: indexPath) as! PickerTableViewCell | |
let cellLimit:CGFloat = cell.frame.origin.y + cell.frame.height | |
let pickerViewLimit:CGFloat = pickerView.frame.height + toolbarHeight | |
if(cellLimit >= pickerViewLimit){ | |
print("位置変えたい") | |
UIView.animate(withDuration: 0.2) { | |
tableView.contentOffset.y = cellLimit - pickerViewLimit | |
} | |
} | |
switch(indexPath.row){ | |
case 0: | |
let index = genderArray.findIndex{$0 == cell.rightLabel.text} | |
if(index.count != 0){ | |
pickerView.selectRow(index[0],inComponent:0,animated:false) | |
} | |
case 1: | |
let index = animalArray.findIndex{$0 == cell.rightLabel.text} | |
if(index.count != 0){ | |
pickerView.selectRow(index[0],inComponent:0,animated:false) | |
} | |
case 2: | |
let index = sportsArray.findIndex{$0 == cell.rightLabel.text} | |
if(index.count != 0){ | |
pickerView.selectRow(index[0],inComponent:0,animated:false) | |
} | |
default: | |
pickerView.selectRow(0, inComponent: 0, animated: false) | |
} | |
pickerIndexPath = indexPath | |
//ピッカービューをリロード | |
pickerView.reloadAllComponents() | |
//ピッカービューを表示 | |
UIView.animate(withDuration: 0.2) { | |
self.pickerToolbar.frame.origin.y = self.view.frame.height - self.pickerViewHeight - self.toolbarHeight | |
self.pickerView.frame.origin.y = self.view.frame.height - self.pickerViewHeight | |
} | |
} | |
} | |
extension PickerTableViewController: UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return textArray.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PickerTableViewCell | |
cell.textLabel?.text = textArray[indexPath.row] | |
print(indexPath.row) | |
//初期値 | |
switch(indexPath.row){ | |
case 0: | |
cell.rightLabel.text = currentGender | |
case 1: | |
cell.rightLabel.text = currentAnimal | |
case 2: | |
cell.rightLabel.text = currentSports | |
default: | |
cell.rightLabel.text = "" | |
} | |
return cell | |
} | |
} | |
extension PickerTableViewController: UIPickerViewDelegate { | |
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
let cell = tableView.cellForRow(at:pickerIndexPath) as! PickerTableViewCell | |
switch(pickerIndexPath.row){ | |
case 0: | |
cell.rightLabel.text = genderArray[row] | |
currentGender = genderArray[row] | |
case 1: | |
cell.rightLabel.text = animalArray[row] | |
currentAnimal = animalArray[row] | |
case 2: | |
cell.rightLabel.text = sportsArray[row] | |
currentSports = sportsArray[row] | |
default: | |
print("何もなし") | |
} | |
} | |
} | |
extension PickerTableViewController: UIPickerViewDataSource { | |
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
if(pickerIndexPath != nil){ | |
switch (pickerIndexPath.row){ | |
case 0: | |
return genderArray.count | |
case 1: | |
return animalArray.count | |
case 2: | |
return sportsArray.count | |
default: | |
return 0 | |
} | |
}else{ | |
return 0 | |
} | |
} | |
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { | |
let label = UILabel() | |
label.textColor = UIColor.white | |
label.backgroundColor = UIColor.clear | |
label.textAlignment = .center | |
label.font = UIFont.systemFont(ofSize: 18) | |
switch(pickerIndexPath.row){ | |
case 0: | |
label.text = genderArray[row] | |
case 1: | |
label.text = animalArray[row] | |
case 2: | |
label.text = sportsArray[row] | |
default: | |
print("なし") | |
} | |
return label | |
} | |
func numberOfComponents(in pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment