swift - Swift:UIStackView 中的文本视图不显示
问题描述
我正在尝试创建一个包含 4 列的基本表。我通过创建一个带有自定义单元格的 UITableView 来解决这个问题,该单元格包含一个包含所需文本视图的水平堆栈视图。这是我的 UITableView 代码:
import UIKit
class HistoryTableViewController: UITableViewController {
let cellID = "HistoryColumns"
struct CellData {
let firstColumn: String?
let secondColumn: String?
let thirdColumn: String?
let fourthColumn: String?
}
var data = [CellData]()
override func viewDidLoad() {
super.viewDidLoad()
data = [CellData.init(firstColumn: "Date", secondColumn: "Flight number", thirdColumn: "Type", fourthColumn: "Pre-refuel FOB (kg)")]
tableView.register(FuelHistoryColumnCells.self, forCellReuseIdentifier: cellID)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! FuelHistoryColumnCells
cell.firstColumn = data[indexPath.row].firstColumn
cell.secondColumn = data[indexPath.row].secondColumn
cell.thirdColumn = data[indexPath.row].thirdColumn
cell.fourthColumn = data[indexPath.row].fourthColumn
return cell
}
}
这是我的自定义单元格代码:
import UIKit
class FuelHistoryColumnCells: UITableViewCell {
var firstColumn: String?
var secondColumn: String?
var thirdColumn: String?
var fourthColumn: String?
lazy var firstColumnView: UITextView = {
let textView = UITextView()
textView.font = UIFont.systemFont(ofSize: 15)
textView.backgroundColor = .green
return textView
}()
lazy var secondColumnView: UITextView = {
let textView = UITextView()
textView.font = UIFont.systemFont(ofSize: 15)
textView.backgroundColor = .green
return textView
}()
lazy var thirdColumnView: UITextView = {
let textView = UITextView()
textView.font = UIFont.systemFont(ofSize: 15)
textView.backgroundColor = .green
return textView
}()
lazy var fourthColumnView: UITextView = {
let textView = UITextView()
textView.font = UIFont.systemFont(ofSize: 15)
textView.backgroundColor = .green
return textView
}()
lazy var fifthColumnView: UITextView = {
let textView = UITextView()
textView.font = UIFont.systemFont(ofSize: 15)
textView.backgroundColor = .green
return textView
}()
lazy var stackView: UIStackView = {
let stack = UIStackView(arrangedSubviews: [firstColumnView, secondColumnView, thirdColumnView, fourthColumnView])
stack.axis = .horizontal
stack.distribution = .fillEqually
return stack
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(stackView)
}
override func layoutSubviews() {
super.layoutSubviews()
if let firstColumn = firstColumn {
firstColumnView.text = firstColumn
}
if let secondColumn = secondColumn {
secondColumnView.text = secondColumn
}
if let thirdColumn = thirdColumn {
thirdColumnView.text = thirdColumn
}
if let fourthColumn = fourthColumn {
fourthColumnView.text = fourthColumn
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我确定我在这里遗漏了一些明显的东西,因为表格视图上没有显示任何内容......
解决方案
推荐阅读
- c# - 会话超时时自动重定向 ASP.NET C#(WebForms)
- spring - 带有 OAuth2.0 的 Spring REST 不起作用
- c# - C#:如何在刻度中间停止计时器
- c# - 通过匹配两个字符串但消除动词和其他一些特定单词(如“the, they, there they etc”)来生成结果?
- java - 如何在java中只增加图像宽度
- ios - 在不上传 App Store 的情况下,我们可以在 TestFlight 中上传 iOS 应用多少次?
- java - 如何替换arraylist中的元素包含作为stringbuilder类型的字符串的元素
- java - 将 java xml sax 事件调用转换为 xml 字符串
- asp.net-core - 静态记录器 aspnetcore webapi serilog
- php - PHP 使用 OOP 防止 SQL 注入