This is a TableView click.

Professional full version, please refer to (www.appcoda.com/learnswift/)

The tutorial documentation at the site above is a trial version that needs to be purchased. (Support legal start from me 💪)

Everyone attention, this article is not a tutorial, just a share of me, there are mistakes please point out.

Also first on the renderings

Here we go!

Note that this article is based on iOS 12 and Xcode 10. This chapter is based on the code in the base chapter. If it is not complete, please read the base chapter first. (juejin. Cn/post / 684490…).

Create a new function to achieve the click effect

Add a function to call when a click occurs and populate it.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let optionMenu = UIAlertController(title: nil, message: "what do you want to do", preferredStyle: .actionSheet)
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        optionMenu.addAction(cancelAction)
        present(optionMenu, animated: true, completion: nil)}Copy the code

Let’s go back to the simulator and see what happens

Of course, you can do what you want.

If you want to play in a different way you can modify the preferredSyle in optionMenu, and to modify the effect of the Cancel button, click the Style in Cancel.

Continue this exercise if you feel competent: When I click Call, another dialog pops up.

I’ll post the code at the end of this article.

Implement the selected function

So what we’re going to do here is we’re going to implement multiple selection, and we’re going to do it again in this function tableView(_ tableView: UITableView, didSelectRowAt indexPath: indexPath), and again it’s very simple and it’s only a couple of lines of code.

 let CheckAction = UIAlertAction(title: "Check in", style: .default, handler: {(action: UIAlertAction) - >Void in
            
            letcell = tableView.cellForRow(at: indexPath) cell? .accessoryType = .checkmark }) optionMenu.addAction(CheckAction)
Copy the code

When we open the emulator again, we are surprised to see that the following unselected list rows are also selected as we slide down.

When I scroll up again, I find that the selected line may not be the one we just selected!

This is a Bug! The reason is that in iOS development, lists don’t build up the number you’re asking for, but are reused as you swipe up and down.

Let’s say you have 30 of them in your array and you put them in your list, but instead of creating 30 of them you might create 10 of them or something, and you slide them up and down and reuse them, and that’s to save the computer but that’s what’s causing the situation that we’re in.

As stated in the tutorial documentation, I hate bugs but can’t avoid them.

Bugs are very helpful in improving our technology.

Our solution to this setup is to set an array that is true if selected and false otherwise. So let’s first define a Boolean array resraurantIsVisited under the entire class.

Then add a bunch of code to the function in tableView (_ didSelectRowAt 🙂

    self.restaurantIsVisited[indexPath.row] = true
Copy the code

Also add judgment code to tableView (_ cellForRowAt 🙂 to remove unselected list rows.

        if restaurantIsVisited[indexPath.row]{
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
Copy the code

Start the emulator again and check the slide to find the problem solved!

Now that the main content of this article is over, you can think about these questions.

1. Click the selected row and Check in becomes Undo Check. Click Undo Check to deselect the Check. 2. The selected list row is a tick mark. Try to change it to another mark.

Finally post the code from the previous exercise adding multiple code boxes:

 
    let CallActionHandler = {(action: UIAlertAction) - >Void in
        let alertMessge = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later", preferredStyle: .alert)
        
        alertMessge.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))self.present(alertMessge, animated: true, completion: nil)}let CallAction = UIAlertAction(title: "Call" + "123-000 -\(indexPath.row+1)", style: .default, handler: CallActionHandler)
    optionMenu.addAction(CallAction)
Copy the code