繁体   English   中英

如果未选择tableView单元格时按下按钮,则显示警报消息

[英]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是否为空,可以检查用于填充tableViewarray / model是否为空。

假设arr是我们用作tableView dataSourcearray

var arr = [String]()

2.接下来,对于tableView选定的indexPaths ,您只需要检查indexPathsForSelectedRows是否为空。

如果为空,则显示UIAlertController ,否则根据您的需求导航到另一个UITableViewController

现在,结合以上所有条件, @IBActionnextButton就像这样,

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM