Ios 如何对不同的数据多次使用同一UIPickerView?-敏捷的

Ios 如何对不同的数据多次使用同一UIPickerView?-敏捷的,ios,swift,uitableview,uipickerview,Ios,Swift,Uitableview,Uipickerview,我在可扩展的UITableViewCell中有一个UIPickerView和一个标签,在代码中我创建了1个部分和3个单元格,它们都与第一个单元格相似 我确实管理了每个单元格的数据,但我的问题是,当我从第一个选择器中选择某个内容,然后转到第二个选择器并选择某个内容时,我的第一个选择将从第二个选择器更改为相同的行顺序 简而言之,如何使3个选择器彼此分离,并将每个单元格的标签从同一单元格更改为选定行,而不是从其他单元格更改为选定行 这是我的视图的屏幕截图: 这是我的代码: import UIKit

我在可扩展的UITableViewCell中有一个UIPickerView和一个标签,在代码中我创建了1个部分和3个单元格,它们都与第一个单元格相似

我确实管理了每个单元格的数据,但我的问题是,当我从第一个选择器中选择某个内容,然后转到第二个选择器并选择某个内容时,我的第一个选择将从第二个选择器更改为相同的行顺序

简而言之,如何使3个选择器彼此分离,并将每个单元格的标签从同一单元格更改为选定行,而不是从其他单元格更改为选定行

这是我的视图的屏幕截图:

这是我的代码:

import UIKit

let cellID = "cell"

class callViewController: UITableViewController   {

var selectedIndexPath : NSIndexPath?
var tapped = false // when the first cell tapped it will be true
var flag = true

// These strings will be the data for the table view cells
let sections: [String] = ["Department:", "Team:", "Service:"]

override func awakeFromNib() {
    super.awakeFromNib()

}
override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self


}

//number of sections
   override func numberOfSectionsInTableView(tableView: UITableView) -> Int     {
    return 1
}
// header for the secation
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Required Info:"
}

//number of rows in section
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}


//get the cell
  override   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! callCustomCell

 cell.titleLabel.text=self.sections[indexPath.row]

// now reload the appropriate pickerData

if indexPath.row == 0{
    cell.inputArrry=cell.departments

    cell.picker.reloadAllComponents()
}

else if indexPath.row == 1{
    cell.inputArrry=cell.teams

 cell.picker.reloadAllComponents()
}


else if indexPath.row == 2{
    cell.inputArrry=cell.services

    cell.picker.reloadAllComponents()
}


      if indexPath.row != 0{

            cell.userInteractionEnabled = tapped

            if (!tapped){
                cell.backgroundColor=UIColor.clearColor()

            }
            else {
                cell.backgroundColor=UIColor.whiteColor()
            }

      }

if indexPath.row==0{
    cell.backgroundColor=UIColor.whiteColor()
    cell.userInteractionEnabled=true

}

return cell

}

// method to run when table view cell is tapped


   override  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

if indexPath.row == 0  && flag {
    flag = false
    tapped = true

   tableView.reloadRowsAtIndexPaths([

        NSIndexPath(forRow: 1, inSection: 0),
        NSIndexPath(forRow: 2, inSection: 0)], withRowAnimation: .Automatic)

}

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

}

  override  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    (cell as! callCustomCell).watchFrameChanges()
}

  override   func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    (cell as! callCustomCell).ignoreFrameChanges()
}

    override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    for cell in tableView.visibleCells as! [callCustomCell] {
        cell.ignoreFrameChanges()
    }
}

   override  func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == selectedIndexPath {
        return callCustomCell.expandedHeight
    } else {
        return callCustomCell.defaultHeight
    }


 }

}
导入UIKit
let cellID=“cell”
类callViewController:UITableViewController{
var selectedIndexPath:NSIndexPath?
var tapped=false//当第一个单元格点击时,它将为true
var flag=true
//这些字符串将是表视图单元格的数据
let节:[String]=[“部门:”、“团队:”、“服务:”]
重写func awakeFromNib(){
super.awakeFromNib()
}
重写func viewDidLoad(){
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
}
//节数
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}
//割台
重写函数tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
返回“所需信息:”
}
//节中的行数
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回3
}
//去拿手机
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell=self.tableView.dequeueReusableCellWithIdentifier(cellID,forIndexPath:indexPath)作为!callCustomCell
cell.titleLabel.text=self.sections[indexPath.row]
//现在重新加载相应的pickerData
如果indexath.row==0{
cell.inputarry=cell.departments
cell.picker.reloadAllComponents()
}
如果indexath.row==1,则为else{
cell.inputarry=cell.teams
cell.picker.reloadAllComponents()
}
如果indexath.row==2,则为else{
cell.inputarry=cell.services
cell.picker.reloadAllComponents()
}
如果indexath.row!=0{
cell.userInteractionEnabled=点击
如果(!点击){
cell.backgroundColor=UIColor.clearColor()
}
否则{
cell.backgroundColor=UIColor.whiteColor()
}
}
如果indexath.row==0{
cell.backgroundColor=UIColor.whiteColor()
cell.userInteractionEnabled=true
}
返回单元
}
//方法在点击表视图单元格时运行
重写func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){
如果indexath.row==0&&flag{
flag=false
抽头=真
tableView.reloadRowsAtIndexPaths([
NSIndexPath(forRow:1,第0节),
NSIndexPath(forRow:2,第0节)],withRowAnimation:。自动)
}
让previousIndexPath=selectedIndexPath
如果indexPath==selectedIndexPath{
selectedIndexPath=nil
}
否则{
selectedIndexPath=indexPath
}
变量索引路径:数组=[]
如果let previous=previousIndexPath{
INDExpath+=[上一页]
}
如果let current=selectedIndexPath{
INDExpath+=[当前]
}
如果indexPaths.count>0{
tableView.reloadRowsAtIndexPaths(InExpaths,withRowAnimation:UITableViewRowAnimation.Automatic)
}
}
重写func tableView(tableView:UITableView,willDisplayCell:UITableViewCell,ForRowatineIndexPath-indexPath:NSIndexPath){
(单元格名称为!callCustomCell).watchFrameChanges()
}
重写func tableView(tableView:UITableView,DiEndDisplayingCell:UITableViewCell,ForRowatineIndexPath indexPath:NSIndexPath){
(单元格名称为!callCustomCell).ignoreFrameChanges()
}
覆盖功能视图将消失(动画:Bool){
超级。视图将消失(动画)
对于tableView.visibleCells as![callCustomCell]中的单元格{
cell.ignoreFrameChanges()
}
}
重写func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat{
如果indexPath==selectedIndexPath{
return callCustomCell.expandedHeight
}否则{
返回callCustomCell.defaultHeight
}
}
}

使用序列图像板中属性检查器选项卡中的标记,然后使用if语句更改返回的内容

if (pickerView.tag == 0) {
    // Do something
} else if (pickerView.tag == 1 {
    // Do something 
} else {
    // Do something
}

您甚至可以使用switch语句。可能看起来更干净

我希望在调用
cell.picker.reloadAllComponents()
@Grimxn时,您还需要保存每个选择器的状态并重置所选组件。我可以保存用户从每个选择器中选择的选项,但是如果我更改了其他选择器的部分,那么选择器中选择的更改又如何呢?或者你能告诉我你指的是什么样的拾荒者。。除了选择,你喜欢什么。。Thank youTry使用此库,它将使用不同的模式字符串、日期和时间为您显示选择器,只需为其提供您要显示的值,并且您有完成处理程序来处理用户的选择: