我正在成功地从API获取textLabel值。但无法在viewForHeaderInSection中设置。在我的应用程序中,在不同的索引处有不同的部分,这意味着有时我得到两个部分,有时得到一个部分,有时得到三个部分
我在试这个密码-

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = HeaderView(tableView: self.tblView, section: section)
        headerView.backgroundColor = UIColor.whiteColor()
        let subjectNameLabel = UILabel(frame: CGRect(x: 19, y: 4, width: 282, height: 35))

        subjectNameLabel.textAlignment = NSTextAlignment.Center
        subjectNameLabel.font = UIFont (name: "HelveticaNeue", size: 16)
        subjectNameLabel.textColor = UIColor.whiteColor()
        headerView.userInteractionEnabled = true

        let dataArrayTblView = dataArrayForTableView
        let subjectNameTxtValue = dataArrayTblView.valueForKey("subjectname")

        subjectNameLabel.text = subjectNameTxtValue as? String
        subjectNameLabel.backgroundColor = UIColor(red: 75/255.0, green: 193/255.0, blue: 210/255.0, alpha: 1.0)

        headerView.addSubview(subjectNameLabel)

        return headerView
    }

最佳答案

试试我的密码。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let frame: CGRect = tableView.frame
        let title: UILabel = UILabel(frame: CGRectMake(50, 3, 200, 30))
        title.text = TotalSection[section] as AnyObject! as! String!
        title.textColor = UIColor.whiteColor()
        title.font = UIFont(name: "OpenSans-Semibold",
                            size: 15.0)
        let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
        headerView.addSubview(title)

        headerView.layer.backgroundColor = BUTTON_SUBMIT_LIGHT_BLACK.CGColor
        return headerView
}

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

关于ios - 无法在viewForHeaderInSection [UITableView]中设置textLabel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39909459/

10-12 02:05