class YourViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var data: [DataType] = [] // Replace DataType with the type of your data
override func viewDidLoad() {
super.viewDidLoad()
fetchDataAndReloadTable()
}
func fetchDataAndReloadTable() {
// Call your API to fetch data
// On successful fetch, update your data array and reload the table view
APIService.fetchData { (fetchedData) in
self.data = fetchedData
self.tableView.reloadData()
}
}
@IBAction func addButtonTapped(_ sender: UIBarButtonItem) {
// Show your alert with textfields
// On pressing the save button in the alert, send the post request
APIService.sendPostRequest { (success) in
if success {
self.fetchDataAndReloadTable()
} else {
// Handle error
}
}
}
// ... Your table view data source and delegate methods ...
}
1条答案
按热度按时间yqlxgs2m1#
添加新项后,需要调用reload the data