【问题标题】:Unable to Format the Section Header of UITableView无法格式化 UITableView 的部分标题
【发布时间】:2020-09-23 16:05:23
【问题描述】:

一个视图控制器,其中包含三个表视图。我正在为每个表的格式设置和设置节标题而苦苦挣扎。我的 viewForHeaderInSection 函数附在下面

我的目标是实现类似于图像的目标。努力为每个表视图格式化和设计节标题。使用附加代码获取 Terminating app due to uncaught exception 'CALayerInvalid', reason: 'layer 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
    }

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

【问题讨论】:

    标签: swift uitableview uiviewcontroller uitableviewsectionheader


    【解决方案1】:

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

    Xcode 不会在这一行给你警告或错误:

    return view
    

    因为view 是控制器的现有对象。

    这是对您正在创建的变量使用“内置”对象名称是一种不好的做法的原因之一。

    将函数的顶部更改为:

    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
     }
    

    【讨论】:

    • 非常感谢您的帮助。非常感谢您的帮助和时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2012-04-05
    • 1970-01-01
    相关资源
    最近更新 更多