[英]Display alert message if button is pressed when no tableView cells selected
我有一个tableView,为单元格启用了多个选择。
TableViewController还包含带有BarButtonItem的导航栏。 选择此按钮后,将打开另一个UITableViewController。
我想添加一个参数,如果没有对UITableView进行选择,则nextButton
将触发一条警告消息,提示用户必须选择一行或更多行。
怎么做这样的事情?
当前,在didSelectRowAt方法中,我启用了选中标记附件:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
定义一个变量,用于在控制器中存储索引路径:
var selectedIndexPaths = [IndexPath]()
每当用户选择一行时,将选定的indexPath添加到数组中:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndexPaths.append(indexPath)
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
检查选择的indexPaths是否为空,则什么都没有选择,您可以显示警报:
if selectedIndexPaths.isEmpty {
//Present alert
}
请注意,您没有在原始问题中处理取消选择 ,但流程类似。
定义将存储所选IndexPath
。
var arrSelectedIndexPaths = [IndexPath]()
在cellForRowAt
内部,检查数组是否包含当前的IndexPath
。 如果是,则将accessoryType
.checkmark
设置为.none
否则设置为.none
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")
cell.textLabel?.text = "Hello"
if arrSelectedIndexPaths.contains(indexPath) {
cell.accessoryType = .checkmark
}
else {
cell.accessoryType = .none
}
return cell
}
在didSelectRowAt
内部,首先检查您的数组是否包含选定的IndexPath
。 如果已经选择,则取消选择它,否则在数组中添加选择的IndexPath
。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if arrSelectedIndexPaths.contains(indexPath) {
if let index = arrSelectedIndexPaths.firstIndex(of: indexPath) {
arrSelectedIndexPaths.remove(at: index)
}
}
else {
arrSelectedIndexPaths.append(indexPath)
}
self.tblVW.reloadData() //This will reload entire TableView
//OR
self.tblVW.reloadRows(at: [indexPath as IndexPath], with: .none) // This will reload only selected Row
}
您的Button动作代码是这样的。
@IBAction func YOUR_BUTTON_TRIGER_ACTION(_ sender: Any) {
if arrSelectedIndexPaths.count <= 0 {
//SHOW YOUR ALERT
}
else {
//DO YOUR FURTHER TASK
}
}
注意:不要在didSelectRowAt
内写tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
,因为滚动TABLEVIEW时选择消失或放错了位置。
1.要检查tableView
是否为空,可以检查用于填充tableView
的array
/ model
是否为空。
假设arr
是我们用作tableView
dataSource
的array
,
var arr = [String]()
2.接下来,对于tableView
选定的indexPaths
,您只需要检查indexPathsForSelectedRows
是否为空。
如果为空,则显示UIAlertController
,否则根据您的需求导航到另一个UITableViewController
。
现在,结合以上所有条件, @IBAction
的nextButton
就像这样,
@IBAction func onTapNextButton(_ sender: UIBarButtonItem) {
if arr.isEmpty || (tableView.indexPathsForSelectedRows != nil && !tableView.indexPathsForSelectedRows!.isEmpty) {
//navigate to another UITableViewController
} else {
//Show Alert here...
let alert = UIAlertController(title: "Alert..!!", message: "You must select atleast 1 row before proceeding.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.