我的屏幕由添加和删除按钮组成,如果我点击添加按钮然后打开弹出屏幕我输入字段并点击弹出屏幕上的保存按钮然后添加数据,如果我关闭弹出屏幕然后表格是没有重装。请检查我的代码,如下所示。
class EditTest: UIViewController , UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var position = [AnyObject]()
var company = [AnyObject]()
var location = [AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
loadJsonData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return position.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! customCell
cell.position.text = position[indexPath.row] as? String
cell.company.text = company[indexPath.row] as? String
cell.location.text = location[indexPath.row] as? String
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
PopupController
.create(self)
.customize(
[
.animation(.slideUp),
.scrollable(false),
.backgroundStyle(.blackFilter(alpha: 0.7))
]
)
.didShowHandler { popup in
print("showed popup!")
}
.didCloseHandler { _ in
print("closed popup!")
}
.show(DeleteRow.instance())
}
@IBAction func newExp(_ sender: Any) {
PopupController
.create(self)
.customize(
[
.animation(.slideUp),
.scrollable(false),
.backgroundStyle(.blackFilter(alpha: 0.7))
]
)
.didShowHandler { popup in
print("showed popup!")
}
.didCloseHandler { _ in
print("closed popup!")
}
.show(AddRow.instance())
}
func loadJsonData() {
let url = URL(string: "https://url..../id")
var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringCacheData,timeoutInterval: 10000)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
print(error!)
return
}
do {
if let jsonData = try JSONSerialization.jsonObject(with:data!, options: []) as? [[String:AnyObject]] {
print(jsonData)
for item in jsonData {
if let pos = item["Position"] as? AnyObject{
self.position.append(pos)
}
if let comp = item["Company"] as? AnyObject{
self.company.append(comp)
}
if let location = item["Location"] as? AnyObject{
self.location.append(location)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
} catch let error as NSError {
print(error)
}
}.resume()
}
}
在上面的代码中,当我关闭弹出窗口时,我做了哪些更改来更新表数据。
func saveMethod(sender:UIButton){
let paramaters = ["Id": id,
"Position": pos,
"Company": com,
"Location": loc] as [String : Any]
let url = URL(string: “https://———/save”)!
let session = URLSession.shared
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 {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
DispatchQueue.main.async {
// now update UI on main thread
let alert = UIAlertController(title: “Row”, message: "Created Successfully", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: { _ -> Void in
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
alert.addAction(OKAction)
self.present(alert, animated: true){}
}
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}