swift - 相同颜色的 TableView 和 TableView 单元格 swift

标签 swift uitableview background-color

我有一个包含不同部分的表格 View ,我的单元格有一个调整大小的框架。我想要的背景。表格 View 和单元格是一样的,所以它看起来是统一的,但我不断地在各部分之间获得更清晰的颜色

我试图将单元格和容器的背景设置为红色,但它也显示为绿色

我的 tableViewController 类

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



}

最佳答案

为部分添加标题 View 并为其设置背景颜色。

  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
        }

单元格保持绿色的原因可能是由于使用了与绿色单元格内容 View 大小相同的未知 View 。用 cell xib 更新你的问题以分享它的根本原因

关于swift - 相同颜色的 TableView 和 TableView 单元格 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57705860/

相关文章:

java - 如何在不更改 Netbeans 背景的情况下禁用 JButton

swift - 在 ARImageTrackingConfiguration 中同时跟踪两个对象

objective-c - 如何从单元格的内容 View 中删除 subview ?

ios - 下标使用不明确

ios - 重新加载包含 UIPageViewController 的 DetailViewController

html - CSS 背景颜色不完整,没有正确覆盖整个页面

Css-grid:在容器外流血背景

swift - 线程 1 : signal SIGABRT with SCNView and UIView Conflicting

javascript - 如何使用 Swift 在 Javascript 中单击按钮

ios - 快速在 Google map 中自定义标记