Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView标题与单元格边界对齐_Ios_Swift_Uitableview_Uikit_Uitableviewsectionheader - Fatal编程技术网

Ios UITableView标题与单元格边界对齐

Ios UITableView标题与单元格边界对齐,ios,swift,uitableview,uikit,uitableviewsectionheader,Ios,Swift,Uitableview,Uikit,Uitableviewsectionheader,我正在为我的UITableView生成一个自定义标题视图,它有两条上下水平线,中间有一个UILabel let lineWidth = tableView.bounds.width //This is not correct, will never align with UITableViewCell let offset = (tableView.bounds.width - lineWidth)/2 //This will always yield 0, but my que

我正在为我的UITableView生成一个自定义标题视图,它有两条上下水平线,中间有一个UILabel

    let lineWidth = tableView.bounds.width //This is not correct, will never align with UITableViewCell
    let offset = (tableView.bounds.width - lineWidth)/2 //This will always yield 0, but my question is to compute line width that aligns with UITableViewCell as shown in the image attached to this question.
    let topLine = UIView(frame: CGRect(x: offset, y: 0, width: lineWidth, height: 1))
    topLine.backgroundColor = UIColor.white
    let bottomLine = UIView(frame: CGRect(x: offset, y: 49.0, width: lineWidth, height: 1))
    bottomLine.backgroundColor = UIColor.white
    let label = UILabel(frame: CGRect(x: 0, y: 1.0, width: tableView.bounds.width, height: 48.0))
    label.textColor = UIColor.white

    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 50))
    headerView.addSubview(topLine)
    headerView.addSubview(label)
    headerView.addSubview(bottomLine)
问题:如下图所示,我需要顶行和底行与部分中的UITableViewCell边界对齐。我从上面的代码中得到的是覆盖UITableView整个宽度的水平线。我如何做到这一点

编辑:这里的一些答案描述了一个任意偏移值,但问题的核心是如何计算与部分中的UITableViewCell边界对齐的偏移量?换句话说,我需要进入部分的UITableViewCell的精确宽度


当你减去相同的东西时,你的
偏移量实际上是

let lineWidth = tableView.bounds.width
let offset = (tableView.bounds.width - lineWidth)/2
let topLine = UIView(frame: CGRect(x: offset, y: 0, width: lineWidth, height: 1)) // this line gonna give offset as zero and width of full tableview width
将此更改为以下代码,然后重试

let lineWidth = tableView.bounds.width - 20
let topLine = UIView(frame: CGRect(x: 10, y: 0, width: lineWidth, height: 1))

这似乎是你的最高和底线的问题。根据
offset
的计算,对于顶行和底行,它始终设置为
0
。因此,最好删除该
偏移量
计算,您可以为顶部和底部行的
CGRect
添加一些所需的静态值作为
x

就我们要移动顶部和底部线条的
x
位置而言,不要忘记从顶部和底部线条的宽度中删除
x
位置的附加值

让yourLine=ui查看(帧:cgrct(x:some\u值,y:0,width:Int(lineWidth-(some\u值*2)),height:1))


最佳做法是可以使用一些变量来实现这一点。

如何计算偏移量?该偏移量暂时为零,但问题是如何相对于UITableViewCell边界(未知)计算该偏移量?@DeepakSharma但相对于tableviewCell/页眉,从前导行和尾随行应该保留一些预定义的间距?或者什么是ur偏移的ur参数?as
tableView.bounds.width
表示tableView的整个宽度?我应该用tableView.bounds.width替换什么(在找到值之前这只是一个占位符)?我需要UITableViewCells的宽度精确,但我如何才能做到这一点?@DeepakSharma这很好,但主要是如何找到偏移量,因为
(tableView.bounds.width-lineWidth)
将始终为零,无论是否指定边界。您有哪些方案可以保持偏移量?你想根据什么计算偏移量?请忘记我的代码,用你想要的替换tableView.bounds.width,但我需要与UITableViewCell的行对齐,就这样。偏移量为0,因为之前我尝试了0.8*tableView.bounds.width等值。如何计算偏移量?该偏移量暂时为零,但问题是如何相对于UITableViewCell边界计算它(在报头计算的这一点上未知)?@DeepakSharma问题是偏移量的计算。根据您当前的计算,它将始终为零。还有一个问题,补偿计算的必要性是什么。您可以将一些所需的预定义值赋给变量,并将其赋给行的CGRect的x。请忘记我的代码,用您想要的任何内容替换tableView.bounds.width,但我需要与UITableViewCell的行对齐,仅此而已。偏移量是0,因为之前我尝试了0.8*tableView.bounds.width等值,但每个这样的比例因子都产生了错误的结果。好的,那么上面的代码将帮助您。其中,some_值是您希望尝试的顶部和底部线条的x位置所需的间距。some_值是由分隔符插图(有效左/右边距)设置的值。您甚至可以设置UITableView的自定义值。参考:。因此,无需进行任何计算来获得某个_值。它将是一些静态值。