i有一个表视图作为视图控制器内部的下拉菜单。表视图只是实际视图中的另一个视图,在单击时要更改不透明度。
用户可以使用下拉菜单选择不同的项目以显示。现在,如果用户选择一个项目,我该如何更新视图?由于显然我想立即向用户展示新项目的数据。
我应该选择另一种方法吗?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = carsArray[indexPath.row]
currentCar = item.name
buttonDropdown(self)
defaults.set(currentCar, forKey: "currentCar")
tableView.deselectRow(at: indexPath, animated: true)
}
创建一种更新UI组合数据的方法,并在didSelectItem
func内更改数据时调用它...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = carsArray[indexPath.row]
currentCar = item.name
buttonDropdown(self)
defaults.set(currentCar, forKey: "currentCar")
// update your UI
updateUIComponents(currentCar: currentCar)
tableView.deselectRow(at: indexPath, animated: true)
}
private func updateUIComponents(currentCar: Car) {
confirmButton.setTitle(currentCar.name, for: .normal)
headerLabel.text = currentCar.detail
// etc...
}
如果与TableView相关的实现在同一视图控制器中,则将调用任何项目didSelectIndExpath委托方法的选择。通过此方法,您可以在视图控制器的主视图中更改视图或详细信息。
如果与表格相关的实现在另一个视图控制器中,则可以使用自定义协议将所选项目的数据传递给具有与TabteView相关的实现的控制器。
希望这会有所帮助。