私は配列からいくつかの値を持つtableViewを持っています。そして、セル上でクリッキングすると、pickerViewが表示されます。これはうまくいきますが、最後のセルをクリックしてpickerViewが表示されたら、最後のいくつかのセルが表示されます。 pickerViewがセルをカバーしないように表示されたら、セルを移動したいと思っています(ユーザーがいくつかのオプションをクリックするとApple Healthアプリのようなものが見えます)。事前にpickerViewを表示した後にセルをシフトする方法
Apple Health App example gif to show requested state
import UIKit
class MoreTableVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var tableView: UITableView!
var pickerView: UIPickerView!
let mainLblArr = ["Afghanistan", "Åland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua & Barbuda", "Argentina", "Armenia", "Aruba", "Ascension Island", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh"]
let detailLblArr = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
var pickerArr = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
}
// MARk: TableView functions
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainLblArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ProtoCell") as? MoreCustomCell {
cell.mainLbl.text = mainLblArr[indexPath.row]
cell.detailLbl.text = detailLblArr[indexPath.row]
return cell
} else {
return UITableViewCell()
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set the position of where the picker should appear to bottom of the screen
pickerView.frame = CGRect(x: 0, y: view.frame.size.height - 200, width: self.view.frame.width, height: 200)
// Apply transformation to the view that will make it slide from the bottom
pickerView.transform = CGAffineTransform(translationX: 0, y: 200)
pickerView.backgroundColor = UIColor.lightGray
// Move the pickerView which contains picker to the subView and show it on the screen
view.addSubview(pickerView)
// Animates the view
UIView.animate(withDuration: 0.3, animations: {
self.pickerView.transform = .identity
})
}
// MARK: PickerView functions
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerArr[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerArr.count
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerView.removeFromSuperview()
}
}
ありがとう!どんな助けでも大歓迎です!
こんにちは、 は返事に感謝。私はあなたが示唆したように解決策を見てきましたが、上の例では私はキーボードで作業していません。 Pickerviewは、セルをタップするとトリガーされます。あなたがセルをタップすると、キーボードと同様の解決策はありますか? –
コンテキストを追加するには:pickerViewの目的はセルにラベルテキストとして表示されるいくつかの値を選択することです。 (この部分はまだ実装されていませんが、ちょっとした背景があります)。 –
次に、セルに非表示のテキストフィールドを追加し、デリゲートメソッドで、選択したデータをViewControllerに返します。 didSelectRowでは、この目に見えないテキストフィールドのbecomeFirstReposnderメソッドを呼び出すことができ、アニメーションとピッカーが作成されます。 –