如何使iOS的tableview in tableview thech标题透明



我实际上正在使用表视图以显示一些数据,并且我正在覆盖willDisplayHeaderView来为标头添加自定义样式。一切都很好,除了我无法更改标题标题的背景颜色。这是我的代码:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
    view.backgroundColor = UIColor.clear
    let title = UILabel()
    title.font = UIFont(name: "System", size: 22)
    title.textColor = UIColor(rgba: "#BA0B23")
    title.backgroundColor = UIColor.clear
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font=title.font
    header.backgroundColor = UIColor.clear
    header.textLabel?.textColor=title.textColor
    header.textLabel?.textAlignment = NSTextAlignment.center
    let sepFrame = CGRect(x: 0, y: header.frame.size.height-1, width: header.frame.size.width, height: 1)
    let seperatorView = UIView(frame: sepFrame)
    seperatorView.backgroundColor = UIColor(rgba: "#BA0B23")
    header.addSubview(seperatorView)
}

我不知道我在做什么错,有些帮助将不胜感激。谢谢

在故事板上您可以访问一个表观单元格,给其自定义tableViewCell类,然后根据设计自定义您的单元格,然后在您的视图控制器中您可以像这样返回该单元

   public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
            let cell:CustomHeaderTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "header") as! CustomHeaderTableViewCell
            let title = uniqueOrders.arrOrders[section] as? String
            cell.lblHeaderTitle.text = "Order Id-(title!)"
            return cell
        }

func numberOfSections(in tableView: UITableView) -> Int{
        return uniqueOrders.arrOrders.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.array[section].count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Order Id- (uniqueOrders.arrOrders[section])"
    }

我认为您应该专注于使用ViewForHeaderInsection,而不是WillDisplayHeaderview。这是我要做的:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 110;
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    headerView.backgroundColor = UIColor.clearColor()
    let title = UILabel()
    title.font = UIFont(name: "System", size: 22)
    title.textColor = UIColor(rgba: "#BA0B23")
    title.backgroundColor = UIColor.clear
    headerView.textLabel?.font = title.font
    headerView.backgroundColor = UIColor.clear
    headerView.textLabel?.textColor = title.textColor
    headerView.textLabel?.textAlignment = NSTextAlignment.center
    headerView.addsubView(title)
    return headerView;
}

希望有帮助

最新更新