ios - TableViewCell Swift 中的自定义视图
问题描述
我是 swift 新手,下面是我面临的问题。我有一个表格视图,其中一个单元格是加载自定义视图的 customCell。没有崩溃,但表格单元格的约束和背景颜色未显示。请参考下面的代码
let data = ["Apple", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape", "Banana", "Orange", "Grape"]
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var customView: CustomView!
override func viewDidLoad() {
super.viewDidLoad()
customView.myLabel.text = "Hey hey"
customView.backgroundColor = UIColor.green
tableView.dataSource = self
tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.tableFooterView = UIView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 3 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomTableViewCell else { return UITableViewCell()}
// cell.layoutSubviews()
// cell.layoutIfNeeded()
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
return cell
}
}
}
Custom View:
class CustomView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var myLabel: UILabel!
let kCONTENT_XIB_NAME = "CustomView"
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed(kCONTENT_XIB_NAME, owner: self, options: nil)
contentView.fixInView(self)
}
}
extension UIView
{
func fixInView(_ container: UIView!) -> Void{
self.translatesAutoresizingMaskIntoConstraints = false;
self.frame = container.frame;
container.addSubview(self);
NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
}
}
Custom Tablecell:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var cellContentView: CustomView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
cellContentView.backgroundColor = UIColor.blue
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
当我运行下面的应用程序时,它是如何显示的屏幕截图,请在此处输入图像描述
问题 1:第 3 行是自定义视图,其背景应为自定义单元格类中设置的蓝色
问题 2:在我的ViewController
中,即使将背景颜色设置为绿色,为什么它不显示该颜色。创建视图时在情节提要中使用“橙色”
问题 3:如果我有更多动态控件,如何设置单元格高度。
请指教。我被这里打动了
解决方案
添加你cellContentView
的单元格contentview
Custom Tablecell:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var cellContentView: CustomView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
contentView.addSubview(cellContentView) // add this line
cellContentView.backgroundColor = UIColor.blue
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
推荐阅读
- discord.js - 我需要帮助修复错误,我尝试了多种不同的方法来修复它,但我没有成功
- bash - 如何在 Bash 中的命令的先前 xargs 的管道的 grep 输出上运行类似 xargs 的命令
- delay - VB2019 睡眠/延迟,GUI 仍在更新
- c++ - 将简单十进制转换为十六进制的最佳方法以及 32 位和 64 位的重要性?十进制字符串?转换?
- postgresql - 如何在postgresql中查找用户创建日期以及所有权限
- linux - ansible 模块如何触发特定的 linux 命令?
- javascript - 字体真棒在 CSS 伪代码中不起作用
- ios - 无法在 ios 上选择初始时间值
- powershell - PowerShell 变量有限制吗?
- xamarin - 如何在 xamarin.forms 中解决messagingcenter.subscribe 多次调用