iOS 修改UITableView separator

separator style

UITableView 中的 separator 有三種類型:


typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {

UITableViewCellSeparatorStyleNone, // 沒有分割線

UITableViewCellSeparatorStyleSingleLine, // 單線,默認(rèn)

UITableViewCellSeparatorStyleSingleLineEtched  // 內(nèi)嵌線,只有在 UITableView 為 group 類型時才起作用

}

通過修改 UITableView 的 separatorStyle 屬性修改 separator 的類型,通過修改 separatorColor 屬性修改 separator 的顏色,通過修改 separatorEffect( iOS 8.0 之后) 屬性修改 separator 的顯示特效。


separator inset

修改 separator 到邊緣距離的時候,只需要修改 UITableView 的 separatorInset 屬性就可以了,例如,當(dāng)使用 xib 或 storyBoard 定制 cell 的時候,修改 tableview 的 Separator Inset 為 Custom,然后修改左右邊距。但是最近突然發(fā)現(xiàn)這樣完全不起作用了,即使手動修改 separatorInsetUIEdgeInsetsZero 也不起作用,具體什么原因沒有細(xì)究,然后通過萬能的百度,找到了解決方法:

方法一:


- (void)viewDidLoad {

[super viewDidLoad];

...

...

...

#pragma mark - a 調(diào)整view邊距

// 1.調(diào)整(iOS7以上)表格分隔線邊距

if ([self.MyTableView respondsToSelector:@selector(setSeparatorInset:)]) {

self.MyTableView.separatorInset = UIEdgeInsetsZero;

}

// 2.調(diào)整(iOS8以上)view邊距(或者在cell中設(shè)置preservesSuperviewLayoutMargins,二者等效)

if ([self.MyTableView respondsToSelector:@selector(setLayoutMargins:)]) {

self.MyTableView.layoutMargins = UIEdgeInsetsZero;

}

}

#pragma mark - b 調(diào)整view邊距

//然后在willDisplayCell方法中加入如下代碼:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

#pragma mark - b

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

}

方法二:


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

...

...

...

#pragma mark - a 調(diào)整view邊距

//1.調(diào)整(iOS8以上)tableView邊距(與上面第2步等效,二選一即可)

if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {

cell.preservesSuperviewLayoutMargins = NO;

}

//2.調(diào)整(iOS8以上)view邊距

if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

[cell setLayoutMargins:UIEdgeInsetsZero];

}

return cell;

}

#pragma mark - b 調(diào)整view邊距

//然后在willDisplayCell方法中加入如下代碼:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

#pragma mark - b

if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

[cell setSeparatorInset:UIEdgeInsetsZero];

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,990評論 2 10
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,299評論 3 38
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,624評論 30 472
  • { 24、Sqlite數(shù)據(jù)庫 1、存儲大數(shù)據(jù)量,增刪改查,常見管理系統(tǒng):Oracle、MSSQLServer、DB...
    CYC666閱讀 1,053評論 0 1
  • 一圖抵千言,說的就是有時候一張圖的表達(dá)結(jié)果,強(qiáng)過你說千百句話。這一點都不意外,我們天生就長著擅長視覺的大腦,形狀的...
    過客閱讀 426評論 0 0

友情鏈接更多精彩內(nèi)容