我有一个带有tableView的项目,里面有tableviewcell和pickerView。
当用户点击单元格时它应该展开并显示pickerView,我的问题是当我点击它扩展的单元格但是pickerView不存在时,当我再次点击以将单元格调整为原始大小时,pickerView会显示随着触摸,所以要看到pickerView我需要按住这样的单元格: screenShot如果我移动手指,它会像这样消失:screenShot
如何只需轻轻一按即可完成选择器显示?
这是我的代码:
ViewController类:
import UIKit
let cellID = "cell"
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource {
var Cell : UITableViewCell?
var selectedIndexPath : NSIndexPath?
// These strings will be the data for the table view cells
let sections: [String] = ["Department:", "Team:", "Service:"]
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
//number of section
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//number of rows in section
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sections.count
}
//get the cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! customCell
cell.textLabel?.text = self.sections[indexPath.row]
return cell
}
// method to run when table view cell is tapped
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let previousIndexPath = selectedIndexPath
if indexPath == selectedIndexPath {
selectedIndexPath = nil
}
else {
selectedIndexPath = indexPath
}
var indexPaths : Array<NSIndexPath> = []
if let previous = previousIndexPath {
indexPaths += [previous]
}
if let current = selectedIndexPath {
indexPaths += [current]
}
if indexPaths.count > 0 {
tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! customCell).watchFrameChanges()
}
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! customCell).ignoreFrameChanges()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
for cell in tableView.visibleCells as! [customCell] {
cell.ignoreFrameChanges()
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath == selectedIndexPath {
return customCell.expandedHeight
} else {
return customCell.defaultHeight
}
}
}
这个代码用于我的tableViewCell:
import UIKit
class customCell: UITableViewCell ,UIPickerViewDelegate , UIPickerViewDataSource{
var isObserving = false;
@IBOutlet weak var picker: UIPickerView!
let items: [String] = ["Finally !", "Oh god", "happy"]
override func awakeFromNib() {
super.awakeFromNib()
self.picker.delegate = self
self.picker.dataSource=self
}
class var expandedHeight: CGFloat { get { return 200 } }
class var defaultHeight: CGFloat { get { return 44 } }
func checkHeight() {
picker.hidden = (frame.size.height < customCell.expandedHeight)
}
func watchFrameChanges() {
if !isObserving {
addObserver(self, forKeyPath: "frame", options: [NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Initial], context: nil)
isObserving = true;
}
}
func ignoreFrameChanges() {
if isObserving {
removeObserver(self, forKeyPath: "frame")
isObserving = false;
}
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "frame" {
checkHeight()
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items.count;
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
print(items[row])
return items[row]
}
}
请帮助