2017-04-22 71 views
0

我跟着從這個答案here的指示,設法讓我自定義的UITableView標題部分,像這樣:如何添加自定義UITableView標題部分筆尖的分隔線?

enter image description here

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 ...

enter image description here

我需要添加分隔線,因爲我想展開/摺疊行。非常感謝您的幫助!

回答

1

也許你需要自己添加一個分隔符是這樣的:在的.xib

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 autolayout

+0

嗯,它的工作原理,但只有當我滾動了起來,向下,而不是在插入部分時。我把你的代碼放在'viewForHeaderInSection'委託中。我應該在哪裏插入這段代碼,以便在插入節時添加分隔符? – iamhx

+0

如果您使用xib,您可以嘗試拖動UIView(seperatorView)並配置AutoLayout。 – isaced

+0

是的,我正在使用xib。我不知道配置自動佈局意味着什麼..你能解釋一下嗎?謝謝! – iamhx