首页 > 解决方案 > 无法格式化 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
    }

任何帮助和帮助将不胜感激。谢谢!

标签: swiftuitableviewuiviewcontrolleruitableviewsectionheader

解决方案


在您的viewForHeaderInSection函数中,您正在创建一个每个块内UIView命名的对象。在每个块的末尾,该对象超出范围......它不再存在。view ifif

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
 }

推荐阅读