显示pickerView

时间:2017-10-27 14:15:45

标签: ios swift uitableview uipickerview

我有一个tableView,其中包含一些来自数组的值,当我碰到单元格时,它应该显示一个pickerView。这很好用,但是当我点击最后一个单元格并显示pickerView时,它会覆盖最后几个单元格。 我想在显示pickerView后将单元格移动到不覆盖单元格,这样用户就可以看到单元格(当您单击某些选项时,就像在Apple Health应用程序中一样)。

Apple Health App example gif to show requested state

Current behaviour of the app

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()
}

}

提前致谢!非常感谢任何帮助!

1 个答案:

答案 0 :(得分:-1)

不要使用框架将pickerView添加为子视图。有更好的解决方案。您可以添加选择器视图作为键盘视图,而不是在键盘显示时注册通知并移动scrollView - 它也可以由真棒库IQKeyboardManager自动管理。您将收到免费显示动画的选择器。

我用谷歌搜索了它,这是我找到的第一个教程:https://blog.apoorvmote.com/uipickerview-as-inputview-to-uitextfield-in-swift/