ios - 红色分隔线未出现在 UITableViewCell 中的自定义标题下

标签 ios swift uitableview

我正在使用 viewForHeaderInSection 来实现标题单元格。 我创建了一个单独的 View ,高度为 1,我假设它应该显示为分隔符。

   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! CustomHeaderCell

        cell.opt.text = "Some"
        cell.len.text = "LEN"
        cell.wgt.text = "WGT"

        if !cell.constraintsInstalled {
            let constraints = [
                cell.stackView.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor),
                cell.stackView.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor),
                cell.stackView.topAnchor.constraint(equalTo: cell.contentView.topAnchor),
                cell.stackView.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor)
            ]
            cell.contentView.addConstraints(constraints) //vw.addSubview(headerCell)




let separatorView = UIView(frame: CGRect(x: tableView.separatorInset.left, y: cell.frame.height, width: tableView.frame.width - tableView.separatorInset.right, height: 1))
            separatorView.backgroundColor = UIColor.red
            cell.addSubview(separatorView)
        }


        return cell

        }

最佳答案

首先你应该把它添加到contentView

cell.contentView.addSubview(separatorView)

其次不要使用框架,因为单元格尚未放置,您还应该使用约束

separatorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    separatorView.leadingAnchor.constraint(equalTo:cell.contentView.leadingAnchor),
    separatorView.trailingAnchor.constraint(equalTo:cell.contentView.trailingAnchor),
    separatorView.heightAnchor.constraint(equalToConstant:1),
    separatorView.bottomAnchor.constraint(equalTo:cell.contentView.bottomAnchor)
])

关于ios - 红色分隔线未出现在 UITableViewCell 中的自定义标题下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51689404/

相关文章:

javascript - 确定变量是增加还是减少?

ios - 在 Swift 中存储异步 Cloud Firestore 查询结果

ios - swift : UIPickerViewController delegates not called after Class-Extraction

ios - uitableview 自动尺寸在控制台上返回错误,而在模拟器上看起来不错

ios - 如何在 iOS 存储设置中显示下载的视频

ios - 使用 GPS 坐标在 iOs map 上创建折线

swift - 全局变量似乎失去了值(value)( swift )

swift - 如何播放具有特定 HTTP header 的 TVML 视频?

ios - 如何将 UITableView 中的单元格颜色设置为透明?

objective-c - 如何让 MainThread 等到某些异步操作完成?