gpt4 book ai didi

swift - 变量仅在 sendAsynchronousRequest 中更新

转载 作者:行者123 更新时间:2023-11-30 10:20:30 26 4
gpt4 key购买 nike

我不想做的事情的示例:

var b  = 0

let URL: NSURL = NSURL(string: "[URL to php file]")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL: URL)
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
b = 5
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return b //Return 0
}

我知道在 numberOfRowsInSection 中它返回 0 导致 sendAsynchronousRequest 在之后执行但有没有办法让 numberOfRowsInSection 函数在 sendAsynchronousRequest 之后执行?或者有没有更好的方法让我的变量在 numberOfRowsInSection 函数之前等于 5?

最佳答案

您不想延迟 numberOfRows... 调用,但您可以在收到异步请求中的数据后调用 tableView.reloadData:

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
b = 5
self.tableView.reloadData()
}

这将使 TableView 重新配置自身并再次调用 numberOfRows...

关于swift - 变量仅在 sendAsynchronousRequest 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26950515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com