tableView单元格

时间:2017-08-08 08:25:56

标签: ios swift uitableview swift3 uipickerview

我创建了一个tableview,它获取日期差异并创建tableview单元格的数量,每个表格视图单元格由textview和picker视图组成,而textview则选择值。问题是当我使用PickerView为1位置选择值时,所选值也会反映在其他单元格上

控制器的代码
这里hfDetailsArr包含总天数差异

 --function for getting date difference count  {
  let dateFormatter = DateFormatter()

                    dateFormatter.dateFormat = "dd/MM/yyyy"
                    let currentCalendar = NSCalendar.current

                    dateFormatter.date(from: dateFormatter.string(from:date1 as Date))

                    let temp = currentCalendar.dateComponents([.day], from:  dateFormatter.date(from: dateFormatter.string(from:date1 as Date))!, to:  dateFormatter.date(from: dateFormatter.string(from:date2 as Date))!)

                    hfDetailsArr.removeAllObjects();
                    //print(temp.day!)

                    if !(temp.day! < 0) && (temp.day! != 0) {
                    for i in 0 ..< (temp.day! + 1){
                        let HFDetailsDates = currentCalendar.date(byAdding: .day, value: i, to: date1 as Date)
                        DataDetails = dateFormatter.string(from: HFDetailsDates!)
                        hfDetailsArr.add(DataDetails)
                         ApplyForDate.append( dateFormatter.string(from: HFDetailsDates!) + ",")

                        }
                //print(ApplyForDate)
                    }
            HFDEtailsTable.reloadData()

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if hfDetailsArr.count < 0 && hfDetailsArr.count != 0 {
        return 0
    }else{
    return hfDetailsArr.count

    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell : HFDetailsCell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! HFDetailsCell

    cell.txtHFDateFeild.text = hfDetailsArr[indexPath.row] as? String
       cell.cellDelegate = self
    return cell

}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("indeddsdsd \(indexPath.row)")

}

其中包含pickerview的UITableview单元格的代码

protocol HFDetailsCellDelegate : class {
    func ValueSelectedChage(sender:HFDetailsCell ,Id:String ,selectedCell:String)
}

class HFDetailsCell: UITableViewCell,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{

    @IBOutlet var txtHFPicker: UITextField!
    @IBOutlet var txtHFDateFeild: UITextField!
   @IBOutlet  var pickerView: UIPickerView! = UIPickerView()
   var HfDtailsOneArr = ["Select","Full Day Leave","Half Day Leave"]
    var position = 0
    //var intHFStatusDetail = ""
   /// internal static var HFtableView = UITableView()
    weak var cellDelegate: HFDetailsCellDelegate?
  //var LeaveDataIDArr = [String?]()

    override func awakeFromNib() {
        txtHFDateFeild.delegate = self
        pickerView.delegate = self
        txtHFPicker.inputView  = pickerView
        pickerView.backgroundColor = UIColor.white
        super.awakeFromNib()
        // Initialization code


    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func txtleaveType(_ sender: UITextField) {



    }


 //print("buttonclick")

//    //UIpickerView
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    //Set number of rows in components

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return self.HfDtailsOneArr.count

    }
    //Set title for each row
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return HfDtailsOneArr[row]


    }
    // Update textfield text when row is selected
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
         //  self.cellDelegate?.ValueSelectedChage(sender:self, Id: HFDetailsLeaveCount(position: row), selectedCell: "")
        txtHFPicker.text = HfDtailsOneArr[row]
       print("pickerText:\(txtHFPicker.text!)")

    }
        func HFDetailsLeaveCount(position:Int)-> String {

            switch position {
            case 0:
                return "0"
            case 1:
                return "8"
            case 2:
                return "4"
            default:
                return "nil"
            }

    }
 }

Image1

Image2

在上面的图像中,如果我选择04/08日期的值,那么相同的值将反映在12/08

- 任何形式的帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:2)

表格单元格中的

,选择器选择调用ValueSelectedChage委托方法

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       self.cellDelegate?.ValueSelectedChage(sender:self, Id: HFDetailsLeaveCount(position: row), selectedCell: HfDtailsOneArr[row])
       txtHFPicker.text = HfDtailsOneArr[row]
       print("pickerText:\(txtHFPicker.text!)")
}

在viewcontroller中创建一个变量以保持选定的值

var hfSelectedDetails: [String: String?]?

在viewcontroller中实现委托方法

func ValueSelectedChage(sender:HFDetailsCell ,Id:String ,selectedCell:String) {
     let indexpath = self.tableView.indexPathForRowAtPoint(cell.center)
     let key = "\(indexpath.row)"
     hfSelectedDetails[key] = selectedCell
     ....//other code if any
}

并在cellForRow添加

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell : HFDetailsCell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! HFDetailsCell

    cell.txtHFDateFeild.text = hfDetailsArr[indexPath.row] as? String
    cell.cellDelegate = self
    let key = "\(indexPath.row)"
    if let selectedValue = hfSelectedDetails[key] {
        cell.txtHFPicker.text = selectedValue
    } else {
        cell.txtHFPicker.text = nil
    }
    return cell

}