4

我正在尝试在标题部分视图中添加部分标题标签和按钮。但它看起来是空的。当我运行应用程序时,标题为空。第二部分代码工作正常

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if (section == 0){
            let label = UILabel.init(frame: CGRect.init(x: 17, y: 139, width: tableView.frame.size.width, height: 45))
                       label.textColor = UIColor.black
                       label.font = UIFont.systemFont(ofSize: 13.0)
                       label.textAlignment = .left
                       label.text = "   My Balances"
                       label.backgroundColor  = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
                       
                       let frame = tableView.frame
                       let height:CGFloat = 66

                       let button = UIButton(frame: CGRect(x: 306, y: 139, width: 15, height: 15))  // create button
                       button.tag = section
                       // the button is image - set image
                       button.setImage(UIImage(named: "remove_button"), for: .normal)

                       let headerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: height))  // create custom view
                       headerView.addSubview(button)   // add the button to the view
                       headerView.addSubview(label)
                       return headerView
                       //return label
                       
            //return label
            
        }
        else {
             let label = UILabel.init(frame: CGRect.init(x: 0, y: 241, width: tableView.frame.size.width, height: 45))
            label.textColor = UIColor.black
            label.font = UIFont.systemFont(ofSize: 13.0)
            label.textAlignment = .left
            label.text = "   My Customers"
            label.backgroundColor  = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
            
            return label
        }
    }
4

1 回答 1

2

你没有遵循正确的方法。heightForHeaderInSection首先,您必须使用from tableview 对象设置标题视图的高度viewDidLoad()-

tableView.heightForHeaderInSection = 250 

或通过使用它的委托方法 -

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 250
    }

您将标题视图的高度设置为等于表格视图的高度。设置它小于它let height:CGFloat = 250-

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
            let label = UILabel.init(frame: CGRect.init(x: 0, y: 241, width: tableView.frame.size.width, height: 45))
            label.textColor = UIColor.black
            label.font = UIFont.systemFont(ofSize: 13.0)
            label.textAlignment = .left
            label.text = "   My Balances"
            label.backgroundColor  = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
            
            let frame = tableView.frame
            let height:CGFloat = 250 

            let button = UIButton(frame: CGRect(x: 5, y: 10, width: 15, height: 15))  // create button
            button.tag = section
            // the button is image - set image
            button.setImage(UIImage(named: "remove_button"), for: .normal)

            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: height))  // create custom view
            headerView.addSubview(button)   // add the button to the view
            headerView.addSubview(label)
            return headerView
            //return label
            
        }

或者另一种方法是制作自定义可重用的标题视图,注册为标题视图,最后将其出列。

您可以按照苹果的文档进行第二种方式 - https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections

于 2020-06-30T04:07:28.183 回答