【问题标题】:How do I add a separator line for a custom UITableView header section nib? [duplicate]如何为自定义 UITableView 标题部分笔尖添加分隔线? [复制]
【发布时间】:2017-04-22 06:56:57
【问题描述】:

我按照这个答案here 中的说明操作并设法获得我的自定义 UITableView 标题部分,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "TableSectionHeader", bundle: nil)
    billTableView.register(nib, forHeaderFooterViewReuseIdentifier: "TableSectionHeader")
}

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

    let cell = billTableView.dequeueReusableHeaderFooterView(withIdentifier: "TableSectionHeader")
    let header = cell as! TableSectionHeader
    header.lblPerson.text = array[section].name
    header.lblTotal.text = "$0.00"
    return cell
}

一切正常,但是我需要为这些部分设置分隔线,并且因为该部分是我笔尖的 UIView,所以我无法使用 .separatorStyle...

我需要添加分隔线,因为我想展开/折叠行。非常感谢您的帮助!

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    也许你需要像这样自己添加一个分隔符:

    CGRect seperatorFrame = CGRectMake(0, headerView.frame.size.height-1, tableView.bounds.size.width, 1);
    UIView *seperatorView = [[UIView alloc] initWithFrame:seperatorFrame];
    seperatorView.backgroundColor = [UIColor grayColor];
    [headerView addSubview:seperatorView];
    

    或在 .xib 中使用自动布局:

    【讨论】:

    • 嗯,它有效,但只有当我上下滚动它时,而不是在插入部分时。我将您的代码放在viewForHeaderInSection 代表中。我应该把这段代码放在哪里,以便在插入部分时也添加分隔符?
    • 如果您使用的是xib,您可以尝试拖动一个UIView(seperatorView)并配置AutoLayout。
    • 是的,我正在使用 xib。我不确定配置自动布局意味着什么。你能解释一下吗?谢谢!
    • 啊,nvm。我明白你的意思了。我刚刚添加了另一个 UIView,更改了它的高度并添加了约束。稍微调整一下,现在看起来很完美:) 谢谢!
    • @iamhx 是的,我就是这个意思
    猜你喜欢
    • 2013-03-14
    • 2023-03-14
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多