UITableView separation between Swift cells

1

I have the following UITableView

I would like to be able to add spaces between each cell

My Swift code

    // MARK: - Table view data source
    func numberOfSections(in tableView: UITableView) -> Int {
        return 6
    }



    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch (section)
        {
        case 0:
            return 1
        case 1:
            return 1
        case 2:
            return 0
        default:
            return 0
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var numberOfRows : Int
        switch (section)
        {

        case 1:
            numberOfRows = 6
            break
        case 2:
            numberOfRows = 0
            break
        default:
            numberOfRows = 0
            break
        }
        return numberOfRows
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



        let mainCell = Bundle.main.loadNibNamed("MenuOptionsTableViewCell", owner: self, options: nil)?.first as! MenuOptionsTableViewCell
        mainCell.selectionStyle = .none
        mainCell.backgroundColor = Utils.Color.COLOR_E8E8E8

        mainCell.strLblTitleOptionsMenu.textColor = Utils.Color.COLOR_2B5034
        switch (indexPath.section)
        {

        case 1:
            if (indexPath.row == 0)
            {

                mainCell.strLblTitleOptionsMenu.text=NSLocalizedString("strLblOption1",comment:"")
                mainCell.imgOptionsMenu.image = UIImage.init(named:"PagarMenu")
                return mainCell
            }
            else if(indexPath.row == 1)
            {

                mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption2",comment:"")
                mainCell.imgOptionsMenu.image = UIImage.init(named:"BillingMenu")
                return mainCell
            }
            else if(indexPath.row == 2)
            {
                mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption3",comment:"")
                mainCell.imgOptionsMenu.image = UIImage.init(named:"CollectionNotificationsMenu")
                return mainCell
            }
            else if(indexPath.row == 3)
            {
                mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption4",comment:"")
                mainCell.imgOptionsMenu.image = UIImage.init(named:"billingQueryMenu")
                return mainCell
            }
            else if(indexPath.row == 4)
            {
                mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption5",comment:"")
                mainCell.imgOptionsMenu.image = UIImage.init(named:"MovsBalanceMenu")
                return mainCell
            }
            else if(indexPath.row == 5)
            {
                mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption6",comment:"")
                mainCell.imgOptionsMenu.image = UIImage.init(named:"CashOutMenu")
                return mainCell
            }
            break
        default:
            break
        }
        return mainCell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

            return 40
           }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        switch (indexPath.section)
//        {
//        case 0:
//            break
//        case 1:
//            if (indexPath.row == 0)
//            {
//                self.presenter.personalInfo()
//            }
//            else if(indexPath.row == 1)
//            {
//                self.presenter.secretQuestion()
//            }
//            else if(indexPath.row == 2)
//            {
//                self.presenter.passwordUpdate()
//            }
//            break
//        case 2:
//            if (indexPath.row == 0)
//            {
//                self.presenter.userLock()
//            }
//            else if(indexPath.row == 1)
//            {
//                self.presenter.unroll()
//            }
//            else if(indexPath.row == 2)
//            {
//                self.presenter.disassociate()
//            }
//            else if(indexPath.row == 3)
//            {
//                self.presenter.signOff()
//            }
//            break
//        default:
//            break
//        }
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

I would like to be able to achieve this effect

    
asked by Bruno Sosa Fast Tag 05.04.2018 в 22:00
source

1 answer

3

An easy way to achieve this effect is to put each cell in a different section and use headers to increase the space between them. With your code it would look like this.

// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
    return 6
}


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 10 // Espacio que deseas tener.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let mainCell = Bundle.main.loadNibNamed("MenuOptionsTableViewCell", owner: self, options: nil)?.first as! MenuOptionsTableViewCell
    mainCell.selectionStyle = .none
    mainCell.backgroundColor = Utils.Color.COLOR_E8E8E8

    mainCell.strLblTitleOptionsMenu.textColor = Utils.Color.COLOR_2B5034
    switch (indexPath.section) {
    case 0:
        mainCell.strLblTitleOptionsMenu.text=NSLocalizedString("strLblOption1",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"PagarMenu")
        return mainCell
    case 1:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption2",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"BillingMenu")
        return mainCell
    case 2:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption3",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"CollectionNotificationsMenu")
        return mainCell
    case 3:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption4",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"billingQueryMenu")
        return mainCell
    case 4:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption5",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"MovsBalanceMenu")
        return mainCell
    case 5:
        mainCell.strLblTitleOptionsMenu.text = NSLocalizedString("strLblOption6",comment:"")
        mainCell.imgOptionsMenu.image = UIImage.init(named:"CashOutMenu")
        return mainCell
    default:
        return UITableViewCell()
    }
    return mainCell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
    
answered by 06.04.2018 / 16:56
source