iOS tableView delegate methods in swift 3

The below are the new table view delegate methods in Swift3. 


   You can display the number of sections here.Now I used as a single section.
   func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }



  For Every section, you can display the number of rows.You can display the rows based on your input array count
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return array.count
    }
 


    You can configure the cell using this method.you can design a customized cell and connect their properties and use it here
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCell
        return cell
    }
 

Based on the cell selection you can perform the operations here.Maximum you can call the nextViewController

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = self.storyboard?.instantiateViewController(withIdentifier: "Cell") as! ViewController
        self.navigationController?.pushViewController(cell, animated: true)
    }




It will return the possibility of a cell can be editable or non-editable.If true it will be editable.Otherwise nonEditable
    // Support conditional editing of the table view.
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
 


Editing style will be used here.If you want to delete the cell then you can use alert view controller to get the confirmation from the user.
    // Support editing the table view.
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {

            // Delete the row from the data source
            
            let alert = UIAlertController(title: AppTitle, message: "title", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.default) {
                UIAlertAction in
            //paste the action here
                }
            }

            let cancelAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.cancel) {

                UIAlertAction in
            }

            alert.addAction(okAction)

            alert.addAction(cancelAction)
            // Present the controller
            self.present(alert, animated: true, completion: nil)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }




    }

Comments

Post a Comment

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift