gpt4 book ai didi

ios - 更改表格 View 中显示文本的颜色

转载 作者:行者123 更新时间:2023-11-28 05:51:48 27 4
gpt4 key购买 nike

我创建了一个 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
}

最佳答案

从问题来看,您似乎正在尝试更改标题 View 的外观。将您的 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
}

关于ios - 更改表格 View 中显示文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52692069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com