http post 请求后重新加载表视图

问题描述 投票:0回答:1

我有主要的 vc 包括 tableview。当我打开我的应用程序时,它开始从 api 获取数据以显示表视图。我在导航栏上有一个添加按钮。当我点击它时,警报带有文本字段。然后我用保存按钮发送帖子请求那些文本字段。当我关闭应用程序并返回时,我在表视图上看到数据,没有问题。但无法自动重新加载。

有人可以帮忙吗?

swift tableview httprequest reload
1个回答
0
投票

添加新项目后需要调用重新加载数据

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 ...
}
© www.soinside.com 2019 - 2024. All rights reserved.