swift3中的TableView重载(刷新)问题?

时间:2017-11-17 13:00:32

标签: swift3 reloaddata

我从选择器中选择值,它在表格中显示值。但是我从选择器中选择了第二个值,它显示了之前的值和新值。但我只想要新的价值观。请查看我的下图。

enter image description here

类ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITableViewDataSource,UITableViewDelegate {     @IBOutlet弱var tableView:UITableView!

@IBOutlet weak var customPicker: UIPickerView!
var pickerDataSource = ["SelectOne","First","Second","Third"];
 var m = ""
 var wln = [AnyObject]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    customPicker.delegate=self
    customPicker.dataSource = self
    tableView.backgroundColor = UIColor(red:239 / 255, green: 242 / 255, blue: 247 / 255, alpha: 1)
    tableView.separatorColor = UIColor.clear

    tableView.dataSource = self

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return wln.count
}
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDataSource.count;
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerDataSource[row] as String
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    m  = pickerDataSource[row]
    tableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell:TableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    print("dd\(self.wln)")

       cell.lbl.text = self.wln[indexPath.row] as? String
        return cell as TableViewCell
    }

  @IBAction func get(_ sender: Any) {
    print("\(m)")
    if(m == "")  {

    }else if(m == "First")  {
        m = "background"
        DispatchQueue.main.async {
            self.tableView.reloadData()
            self.viewDidLoad()
        }

    }
    else if(m == "Second")  {
        print("drugTest")
        m = "drugTest"
        DispatchQueue.main.async {

            self.tableView.reloadData()
            self.viewDidLoad()
        }

    } else if(m == "Third")  {

        print("Other Details")
         m = "otherDetails"
        DispatchQueue.main.async {
            self.tableView.reloadData()
            self.viewDidLoad()
        }

    }
    print("Ggg\(m)")

    let paramaters = [ "Id":"300724","Type":m]

   let url = URL(string: "http://-------")
            //create the session object
            let session = URLSession.shared

            //now create the URLRequest object using the url object
            var request = URLRequest(url: url!)
            request.httpMethod = "POST" //set http method as POST

            do {
                request.httpBody = try JSONSerialization.data(withJSONObject: paramaters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body

            } catch let error {
                print(error.localizedDescription)
            }

            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("application/json", forHTTPHeaderField: "Accept")

            //create dataTask using the session object to send data to the server
            let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

                guard error == nil else {
                    return
                }

                guard let data = data else {
                    return
                }

                do {

                    let httpResponse = response as? HTTPURLResponse
                    print(httpResponse)

                    let myV = httpResponse?.statusCode

                    if myV == 200{

                        if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? Array<AnyObject> {
                        DispatchQueue.main.async {
                               for item in json{

                             if let img1 = item["Name"] as? AnyObject {
                                self.wln.append(img1)
                                DispatchQueue.main.async {
                                    self.tableView.reloadData()
                                    self.viewDidLoad()
                                }
                            }
                            }
                        }
                    }
                }
                } catch let error {
                    print(error.localizedDescription)
                }
            })
            task.resume()
}

}

0 个答案:

没有答案