GDes00
GDes00

Reputation: 285

Table view and tableview cell of the same color swift

I have a table view with different sections, my cell have a resized frame. I would like the background of the . table view and the cell to be the same so it appears uniform but I keep getting a clearer color between the sections

I tried to set the background of the cell and the container to be red but it says green as well

My tableViewController class

import UIKit

class StrainTableViewController: UITableViewController {


    //MARK: Properties

    var diseaseImage = [UIImage(named: "Anxiety"), UIImage(named: "Depression"), UIImage(named: "Arthritis"), UIImage(named: "Cramps"), UIImage(named: "Fatigue"), UIImage(named: "Headache")]
    var diseaseName = ["Anxiety", "Depression", "Arthritis", "Cramps", "Fatigue", "Headache"]
    let sectionSpacing:CGFloat = 20
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = 200
        tableView.backgroundColor = UIColor.green
        self.title = "Here you can explore different strains"


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return diseaseImage.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
     override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return sectionSpacing
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "StrainCell", for: indexPath) as! StrainTableViewCell

        // Configure the cell...

        let image = diseaseImage[indexPath.section]
         cell.diseaseImage.image = image
        cell.layer.cornerRadius = 8
        cell.backgroundColor = UIColor.red
        cell.container.layer.backgroundColor = UIColor.red



        return cell
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } 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
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */


    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        super.prepare(for: segue, sender: sender)

        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        let selectedCell = sender as! StrainTableViewCell
        let vc = segue.destination as! StrainListTableViewController
        let index = tableView.indexPath(for: selectedCell)
        vc.condition = diseaseName[index!.section]

    }


    //MARK: -private methods



}

Upvotes: 0

Views: 115

Answers (2)

Neeraj
Neeraj

Reputation: 91

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: sectionSpacing))

    headerView.backgroundColor = colorYouWantToAssign
    return headerView
}

Upvotes: 0

Jeesson_7
Jeesson_7

Reputation: 811

Add a header view for section and set a background color for it.

  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
        {
            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))           
                headerView.backgroundColor = UIColor.red

            return headerView
        }

The reason cell is staying green maybe caused used to an unknown view with same size as cell content view with green color. update ur question with cell xib to share its root cause

Upvotes: 1

Related Questions