swift - 我在哪里从 UIAlertController 的选择中重新加载 tableView?
问题描述
我有UITableView
一些滑动动作。其中一个通过 显示操作表UIAlertController
,并让用户更改 a 的类别UITableViewCell
,使单元格出现在另一个部分中。
我试图实现的是在做出选择后重新加载 tableView,但似乎警报是异步调用的。有人可以帮我理解调用堆栈以及tableview.reloadData()
调用的位置吗?
let switchTypeAction = UIContextualAction(style: .normal, title: nil) { [weak self] (_,_,success) in
let ac = UIAlertController(title: "Change type", message: nil, preferredStyle: .actionSheet)
for type in orderItem.orderItemType.allValues where type != item.type {
ac.addAction(UIAlertAction(title: "\(type.prettyName)", style: .default, handler: self?.handleChangeType(item: item, type: type)))
}
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
self?.present(ac, animated: true)
tableview.reloadData()
success(true)
}
func handleChangeType(item: orderItem, type: orderItem.orderItemType) -> (_ alertAction:UIAlertAction) -> (){
return { alertAction in
item.type = type
}
}
我会假设调用是按这个顺序执行的,但是在调试时我看到它self?.present(ac, animated: true)
实际上是在块之后执行的,所以首先执行重新加载和成功响应。这与关闭有什么关系吗@escaping?
解决方案
是的,UIAlertController
是“异步”的,因为呈现警报的 VC 在呈现警报时仍将运行代码。事实上,对于任何展示另一个 VC 的 VC,这通常都是正确的。
您应该调用接受reloadData
的handler
闭包UIAlertAction.init
。这是用户选择操作时将调用的闭包。在这里,您正在传递 的返回值handleChangeType
,因此您应该将其写在这里:
func handleChangeType(item: orderItem, type: orderItem.orderItemType) -> (_ alertAction:UIAlertAction) -> (){
return { alertAction in
item.type = type
self.tableView.reloadData()
}
}
如果您知道移动项目的索引路径,则可以moveTow(at:to:)
改为仅重新加载这两行,而不是使用所有内容。
推荐阅读
- excel - 需要修复一个excel单元格,但是当我复制并粘贴下面的行时,固定单元格应该改变
- python - 使用 .loc 不一致地切片列
- smtp - BizTalk SMTP 消息“msg_Email”的“PartAttachment”部分在构造块的末尾包含空值
- mysql - 如何使用特定规则将 TOP 3 值选择到 3 列?
- json - JSON 模式不验证缺少的属性
- java - 为什么 Eclipse 中的 Codota 插件不起作用?
- c - fopen 读取无效的参数 C
- php - 使用邮递员的文件导出 HTTP POST 请求不起作用
- c - 如何在另一个数组中声明一个数组而不定义大小
- c# - Ozeki VoIP SDK 捕捉电话的新闻事件