简体   繁体   English

更改表格视图中显示文本的颜色

[英]Change the color of displayed text in table view

I created a JSON File and displayed it inside a tableViewController.我创建了一个 JSON 文件并将其显示在 tableViewController 中。 Everything should be in my custom color.一切都应该是我的自定义颜色。

Here's an image: https://www.imagebanana.com/s/1203/xWWzUNMs.html这是一张图片: https : //www.imagebanana.com/s/1203/xWWzUNMs.html

My problem is: I can't change the color from the black text.我的问题是:我无法更改黑色文本的颜色。

My second problem is: When I scroll down, the black text is inside of my titles.我的第二个问题是:当我向下滚动时,黑色文本在我的标题内。 Is there any change to fix the position from the text?是否有任何更改可以从文本中修复位置?

I tried many things but I'm stucked.我尝试了很多东西,但我被卡住了。

This is my code(tableViewController):这是我的代码(tableViewController):

import UIKit

class CategoryTableViewController: UITableViewController {

    @IBOutlet weak var searchBarAnswer: UISearchBar!

    var categoryNumber: Int?
    var answers: [Dictionary<String, AnyObject>]?
    var answersSearchResult: [Dictionary<String, AnyObject>]?
    var arrayQuestions: [Dictionary<String, AnyObject>]?
    var sectionTitle: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedSectionHeaderHeight = 80
        tableView.sectionHeaderHeight = UITableViewAutomaticDimension

        if let path = Bundle.main.path(forResource: "data", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
                if let jsonResult = jsonResult as? Dictionary<String, AnyObject>, let questions = jsonResult["questions"] as? [Dictionary<String, AnyObject>] {
                    arrayQuestions = questions;
                    for question in questions {
                        if let id = question["id"] as? Int {
                            if(categoryNumber! == id)
                            {
                                answers = question["answers"] as? [Dictionary<String, AnyObject>]
                                sectionTitle = question["title"] as? String
                            }
                        }
                    }
                     answersSearchResult = answers;
                }
            } catch {
                // handle error
            }
        }

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 100))

    let label = UILabel()
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
    label.text = sectionTitle

    headerView.addSubview(label)

    return headerView
}


override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return self.answersSearchResult?.count ?? 0
}

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

    let answer = answersSearchResult![indexPath.row]
    cell.categoryLabel.text = answer["title"] as? String

    let type = answer["type"] as! String
    if(type == "question") {
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    }


    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    searchBarAnswer.resignFirstResponder()
    let answer = answersSearchResult![indexPath.row]
    let type = answer["type"] as! String
    if(type == "question") {
        let link = answer["link"] as! Int
        if let viewController2 = self.storyboard?.instantiateViewController(withIdentifier: "CategoryTableViewController") {
            let categoryTableViewController = viewController2 as! CategoryTableViewController
            categoryTableViewController.categoryNumber = link
            self.navigationController?.pushViewController(viewController2, animated: true)
        }
    } else {
        let link = answer["link"] as! String
        if let viewController2 = self.storyboard?.instantiateViewController(withIdentifier: "PDFViewController") {
            let pdfViewController = viewController2 as! PDFViewController
            pdfViewController.pdfName = link
            self.navigationController?.pushViewController(viewController2, animated: true)
        }
    }

}

And this is my tableViewCell:这是我的 tableViewCell:

import UIKit
 class CategoryTableViewCell: UITableViewCell {

@IBOutlet weak var categoryLabel: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

From the question, it looks like you are trying to change the header view's appearance.从问题来看,您似乎正在尝试更改标题视图的外观。 Update your viewForHeaderInSection as:将您的viewForHeaderInSection更新为:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 100))
     // You can change headerView's color as you want here
    headerView.backgroundColor = UIColor.init(red: 111.0/255.0, green: 111.0/255.0, blue: 111.0/255.0, alpha: 1.0)

    let label = UILabel()
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.frame = CGRect.init(x: 5, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
    label.text = sectionTitle
    // Again you can set textColor as you need
    label.textColor = UIColor.yellow 

    headerView.addSubview(label)

    return headerView
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM