我创建了一个JSON文件,并将其显示在tableViewController中。 一切都应该使用我的自定义颜色。
这是一张图片:https://www.imagebanana.com/s/1203/xWWzUNMs.html
我的问题是:我无法从黑色文本更改颜色。
我的第二个问题是:当我向下滚动时,黑色文字在标题内。是否有任何更改可固定文本位置?
我尝试了很多事情,但是被卡住了。
这是我的代码(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)
}
}
}
这是我的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
}
答案 0 :(得分:1)
从该问题来看,您似乎正在尝试更改标题视图的外观。将您的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
}