Swift 如何在api完成调用之前停止加载tableview?(backbutton-case)
我有一个tableview和一个api和api调用函数。收到数据后,我只需对viewDidLoad()和Table.reloadData()使用apicall()函数。当我第一次看到这个屏幕时,它工作得很好 我在tableview中有一个代码Swift 如何在api完成调用之前停止加载tableview?(backbutton-case),swift,api,tableview,Swift,Api,Tableview,我有一个tableview和一个api和api调用函数。收到数据后,我只需对viewDidLoad()和Table.reloadData()使用apicall()函数。当我第一次看到这个屏幕时,它工作得很好 我在tableview中有一个代码 zerointdeparturehour = String(departurehour[indexPath.row]) 它从api调用函数中填充的数组中获取时间 一切正常。视图加载,api被调用,获取数据,tableview重新加载并填充 如果api再次
zerointdeparturehour = String(departurehour[indexPath.row])
它从api调用函数中填充的数组中获取时间
一切正常。视图加载,api被调用,获取数据,tableview重新加载并填充
如果api再次被调用,则使用departurehour.removeAll()删除从api调用创建的所有数组值。(我使用它是为了可以在同一屏幕内针对不同日期反复调用api)
但是,
如果我按backbutton(默认backbutton)返回上一页,并且如果我再次尝试返回此页,viewDidLoad()将调用apicall()函数
但我在
zerointdeparturehour = String(departurehour[indexPath.row]) - Index out of bounds.
为什么只有当我从tableview页移回上一页并再次返回此tableview页时才会发生这种情况?(我知道会发生这种情况,因为departurehour数组目前为零,因为api没有填充其中的信息)
为什么不在第一次发生呢
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return flightcount // comes from api
}
I do have a function to check if the flightcount is 0 or not and what to do when count is 0 .
我也从Api中得到了我的行,它第一次工作
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return flightcount // comes from api
}
I do have a function to check if the flightcount is 0 or not and what to do when count is 0 .
应用程序正在崩溃,因为flightcount保留了上一个屏幕上的值,表试图加载该值,因此崩溃
我修复了它,只需将其重新初始化为0。
override func viewDidAppear(_ animated: Bool) {
flightcount = 0
}
在上一屏幕上。谢谢你给我指明了正确的方向。(两位发表评论的人)有两种可能的解决方案:
departurehour
和flightcount
dataArray,在这两个数组准备就绪之前,不应重新加载tableview
出发时间
之前添加检查,无论其是否为空:if!出发时间{
zerointdeparturehour=String(departurehour[indexPath.row])
}
也为行数部分添加代码。也可以调用API调用。@ Sharad Chauhan,我也从API中得到行计数。所以考虑这个场景,你的飞行计数大于0,所以你将有一个或多个行,然后你访问没有价值的部门,它会崩溃吗?你是用0作为默认值吗?var flightcount=0@Zyfe3r不要创建任何类似flightcount的变量。如果从api获取数据数组,请将其存储在数组和用户数组中。使用numberOfRowsInSection方法计数不要使用!出发时间。isEmpty
。检查departurehour.index.contains(indexPath.row)