swift - 无法格式化 UITableView 的部分标题
问题描述
一个视图控制器,其中包含三个表视图。我正在为每个表的格式设置和设置节标题而苦苦挣扎。我的 viewForHeaderInSection 函数附在下面
我的目标是实现类似图像的目标。努力为每个表视图格式化和设计节标题。由于未捕获的异常'CALayerInvalid',使用附加的代码获取终止应用程序,原因:'layer <CALayer: 0x12cc80610> is a part of cycle in its layer tree'错误消息
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if(tableView == firstTableView){
let button = UIButton(frame: CGRect(x: 270, y: 15, width:20 , height: 20))
button.tag = section
button.setImage(UIImage(named: "InfoIcon"), for: UIControl.State.normal)
button.addTarget(self,action:#selector(buttonClicked),for:.touchUpInside)
let view = UIView(frame: CGRect(x: 20, y: 8, width: tableView.frame.size.width, height: 30))
view.addSubview(button)
let label = UILabel(frame: CGRect(x: 12, y: 7, width: tableView.frame.size.width, height: 30))
label.font = UIFont.systemFont(ofSize: 24)
label.textColor = .white
label.text = "Learn More"
view.addSubview(label)
view.backgroundColor = UIColor.gray // Set your background color
}
else if (tableView == secondTableView) {
let button = UIButton(frame: CGRect(x: 270, y: 15, width:20 , height: 20))
button.tag = section
button.setImage(UIImage(named: "InfoIcon"), for: UIControl.State.normal)
button.addTarget(self,action:#selector(buttonClickedSecond),for:.touchUpInside)
let view = UIView(frame: CGRect(x: 20, y: 8, width: tableView.frame.size.width, height: 30))
view.addSubview(button)
let label = UILabel(frame: CGRect(x: 12, y: 7, width: tableView.frame.size.width, height: 30))
label.font = UIFont.systemFont(ofSize: 24)
label.textColor = .white
label.text = "Get Support"
view.addSubview(label)
view.backgroundColor = UIColor.gray
}
else if (tableView == thirdTableView)
{
let button = UIButton(frame: CGRect(x: 270, y: 15, width:20 , height: 20))
button.tag = section
button.setImage(UIImage(named: "InfoIcon"), for: UIControl.State.normal)
button.addTarget(self,action:#selector(buttonClickedThird),for:.touchUpInside)
let view = UIView(frame: CGRect(x: 20, y: 8, width: tableView.frame.size.width, height: 30))
view.addSubview(button)
let label = UILabel(frame: CGRect(x: 12, y: 7, width: tableView.frame.size.width, height: 30))
label.font = UIFont.systemFont(ofSize: 24)
label.textColor = .white
label.text = "Community"
view.addSubview(label)
view.backgroundColor = UIColor.gray
}
return view
}
任何帮助和帮助将不胜感激。谢谢!
解决方案
在您的viewForHeaderInSection
函数中,您正在创建一个在每个块内UIView
命名的对象。在每个块的末尾,该对象超出范围......它不再存在。view
if
if
Xcode 不会在这一行给你警告或错误:
return view
因为view
是控制器的现有对象。
这是对您正在创建的变量使用“内置”对象名称是一种不好的做法的原因之一。
将 func 的顶部更改为:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 20, y: 8, width: tableView.frame.size.width, height: 30))
if(tableView == firstTableView){
// etc...
并从每个if
块中删除该行。
更好的是,将其更改为:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let myHeaderView = UIView(frame: CGRect(x: 20, y: 8, width: tableView.frame.size.width, height: 30))
if(tableView == firstTableView){
// etc... but change all instances of view. to myHeaderView.
} // end of last if block
return myHeaderView
}
推荐阅读
- html - 弹出按钮中的 HTML 链接
- html - 根据注册的颜色显示表结构
- bash - 具有快速压缩的有效 ORC 文件的最小大小应该是多少
- c++ - 如何在没有内置 UDP 方法的情况下使用 UDP 流式传输 FFmpeg h.264 编码输出
- javascript - Discord bot 在用户响应后无意中发送垃圾邮件
- django - 如何为 django 项目提供默认图像
- php - 如何在商店页面中将 woocommerce 产品图片设置为背景图片
- ionic3 - 如何在 Ionic 3 中实现以下巴士座位布局安排?
- javascript - 为什么我的节点服务器无法从我的节点服务器获得任何响应到 chrome 浏览器,但它在邮递员上运行良好?
- flutter - 如何在 Agora 的 Flutter 生产应用中生成服务器令牌