如何使用相同的UIPickerView多次使用不同的数据? - 斯威夫特

时间:2016-08-26 15:14:32

标签: ios swift uitableview uipickerview

我有一个UIPickerView和一个标签在可扩展的UITableViewCell中,在代码中我制作了1个部分,3个单元格都像第一个单元格。

我确实管理过每个单元格的数据但我的问题是当我从第一个选择器中选择一些东西并且我去第二个选择器并选择一些东西时,我的拳头选择将从第二个选择器更改为相同的行顺序。

为了简化这一点,我如何让3个采摘器彼此分开,并将每个单元格的标签更改为来自同一个单元格的所选行

这是我视图的截图:

screenShot

这是我的代码:

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
    }


 }

}

1 个答案:

答案 0 :(得分:1)

在故事板中的属性检查器选项卡中使用标记,然后使if语句更改返回的内容

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

你甚至可以使用switch语句。可能看起来更清洁。