TableView重新加载数据不起作用编辑更改的值时

时间:2017-06-02 06:18:31

标签: ios swift uitextfield

我正在尝试在UITextField中键入数据时重新加载我的tableview。当我键入某些东西时,重新加载操作将起作用,但是当我稍后删除某些时候重新加载操作不起作用。这是我的代码:

@IBOutlet weak var showErrorlbl: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchforText: UITextField!
@IBOutlet weak var searchforDate: UITextField!
@IBOutlet weak var pickerView: UIDatePicker!
var dispatchArr = [String]()
var dispatch: String = String()
var dismantl: String = String()
var dismantal: [String] = [String]()
var totalSqft: String = String()
var totalSQFT: [String] = [String]()
var marktPerson: [String] = [String]()
var showNameArr: [String] = [String]()
var showName = String()
var number = [String]()
var day = Int()
var month = Int()
var year = Int()
var popup:UIView!
@IBAction func datePicker(_ sender: Any)
{
   if pickerView.isHidden == true
   {
     pickerView.isHidden = false
   }else
   {
     pickerView.isHidden = true
     tableView.reloadData()
     for i in 0 ..< passAllDetails.count
     {
        dispatch = (passAllDetails[i]["Start_Date"] as! String)
        if  dispatch.contains(searchforDate.text!)
        {
            showErrorlbl.isHidden = true
            dispatchArr.append(showName)
            dismantl = (passAllDetails[i]["Dismantel_Date"] as! String)
            totalSqft = (passAllDetails[i]["Board_Size"] as! String)
            let markPerson = (passAllDetails[i]["Marketing_Person_Name"] as! String)
            showName = (passAllDetails[i]["Show_Name"] as! String)
            showNameArr.append(showName)
            number.append(dispatch)
            dismantal.append(dismantl)
            totalSQFT.append(totalSqft)
            marktPerson.append(markPerson)
        }
                }
   }
    DispatchQueue.main.async(execute: { ()->() in
        self.tableView.reloadData()
    })
    //tableView.reloadData()
}
 public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
{
    searchforDate.text = ""
    searchforText.text = ""
            return true
}
   @IBAction func homeButton(_ sender: Any)
{
    self.performSegue(withIdentifier: "searchsegue", sender: self)
}
override func viewDidLoad()
{
    searchforDate.delegate = self
    searchforText.delegate = self
    searchforText.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    searchforDate.addTarget(self, action: #selector(datePicker(_:)), for: .editingChanged)
    pickerView.isHidden = false
    pickerView.timeZone = NSTimeZone.local
    pickerView.backgroundColor = UIColor.gray
    //Add an event to call onDidChangeDate function when value is changed.
    pickerView.addTarget(self, action: #selector(SearchViewController.datePickerValueChanged(_:)), for: .valueChanged)
    super.viewDidLoad()
    //Do any additional setup after loading the view.
}
//Implement in DidChange function
func textFieldDidChange(_ textField: UITextField) {

    for i in 0 ..< passAllDetails.count
    {
        showName = (passAllDetails[i]["Show_Name"] as! String)
        if showName.contains(searchforText.text!)
        {
                showErrorlbl.isHidden = true
                showNameArr.append(showName)
                dispatch = (passAllDetails[i]["Dispatch_Date"] as! String)
                dismantl = (passAllDetails[i]["Dismantel_Date"] as! String)
                totalSqft = (passAllDetails[i]["Board_Size"] as! String)
                let markPerson = (passAllDetails[i]["Marketing_Person_Name"] as! String)
                number.append(dispatch)
                dismantal.append(dismantl)
                totalSQFT.append(totalSqft)
                marktPerson.append(markPerson)
               //tableView.reloadData()
        }
    }

DispatchQueue.main.async(execute: { ()->() in
    self.tableView.reloadData()
})    }
func datePickerValueChanged(_ sender: UIDatePicker){
    let componenets = Calendar.current.dateComponents([.year, .month, .day], from: sender.date)
      day = componenets.day!
     month = componenets.month!
     year = componenets.year!
    searchforDate.text = ("\(day)-\(month)-\(year)")
}
override func viewWillAppear(_ animated: Bool) {
    pickerView.isHidden = true
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
              return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if searchforDate.text == ""
    {
        return showNameArr.count
    }
    else if searchforText.text == ""
    {
        return dispatchArr.count
    }
  return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! SearchCustomCell
    cell.dispatchDateLbl.text = number[indexPath.row]
    cell.marketingPersonName.text = marktPerson[indexPath.row]
    cell.showNameLabel.text = showNameArr[indexPath.row]
    cell.totalSQFTTextview.text = totalSQFT[indexPath.row]
    cell.dismantlDateLbl.text = dismantal[indexPath.row]
    return cell
}

1 个答案:

答案 0 :(得分:0)

试试这个:

func textFieldDidChange(_ textField: UITextField) {
    showNameArr.removeAllData()
    for i in 0 ..< passAllDetails.count
    {
        showName = (passAllDetails[i]["Show_Name"] as! String)
        if showName.contains(searchforText.text!)// and check that this condition will be true while delete character. 
        {
                showErrorlbl.isHidden = true
                showNameArr.append(showName)
                dispatch = (passAllDetails[i]["Dispatch_Date"] as! String)
                dismantl = (passAllDetails[i]["Dismantel_Date"] as! String)
                totalSqft = (passAllDetails[i]["Board_Size"] as! String)
                let markPerson = (passAllDetails[i]["Marketing_Person_Name"] as! String)
                number.append(dispatch)
                dismantal.append(dismantl)
                totalSQFT.append(totalSqft)
                marktPerson.append(markPerson)
        }
            tableView.reloadData()

    }