Display alert message if button is pressed when no tableView cells selected

血红的双手。 提交于 2020-01-16 14:54:46

问题


I have a tableView that has multiple selections enabled for the cells.

The TableViewController also contains a navigation bar with a BarButtonItem. When this button is selected another UITableViewController is opened.

I would like to add a parameter where if no selections are made to the UITableView than the nextButton will trigger an alert message saying that the user must select a row or more.

How can something like this be done?

Currently, in the didSelectRowAt method, I have a checkmark accessory enabled:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

回答1:


1. To check if the tableView is empty, you can check if the array/model that you're using to fill the tableView is empty or not.

Let's suppose arr is the array we're using as dataSource for tableView,

var arr = [String]()

2. Next, for selected indexPaths in the tableView, you simply need to check if indexPathsForSelectedRows is empty or not.

If empty, then show UIAlertController, otherwise navigate to another UITableViewController as per your requirement.

Now, combining all the above conditions, the @IBAction for nextButton goes like,

@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)
    }        
}



回答2:


Define a variable for storing indexpaths in the econtroller:

var selectedIndexPaths = [IndexPath]()

add selected indexPath into array whenever user selects a row:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedIndexPaths.append(indexPath)
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

check if selected indexPaths is empty, then nothing is selected and you can present alert:

if selectedIndexPaths.isEmpty {
    //Present alert
}

Note that you didn't handle Deselect in the original question but the flow is similar.




回答3:


Define array that will store the selected IndexPath.

var arrSelectedIndexPaths = [IndexPath]()

Inside cellForRowAt, check your array contains current IndexPath. If yes then set accessoryType to .checkmark otherwise set to .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
}

Inside didSelectRowAt, first of all check your array contains the selected IndexPath or not. If it already selected then unselect it otherwise add selected IndexPath in array.

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
}

Your Button action code something like this.

@IBAction func YOUR_BUTTON_TRIGER_ACTION(_ sender: Any) {
    if arrSelectedIndexPaths.count <= 0 {
        //SHOW YOUR ALERT
    }
    else {
        //DO YOUR FURTHER TASK
    }
}

NOTE: don't write tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark inside the didSelectRowAt because when you scroll your TABLEVIEW the selection was gone or misplaced.



来源:https://stackoverflow.com/questions/57471793/display-alert-message-if-button-is-pressed-when-no-tableview-cells-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!