国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS利用UITableView設置全屏分隔線的3種方法總結

2020-07-26 02:37:33
字體:
來源:轉載
供稿:網友

前言

本文主要給大家總結了iOS用UITableView設置全屏分隔線的3種方法,一般TableView設置全屏分隔線有下面三種方法:

1.自定義cell,手動添加分割線

隱藏自帶的

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

可以通過addSubview的方式添加一條分割線;也可以自繪分割線。

// 自繪分割線- (void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); CGContextFillRect(context, rect); CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0xE2/255.0f green:0xE2/255.0f blue:0xE2/255.0f alpha:1].CGColor); CGContextStrokeRect(context, CGRectMake(0, rect.size.height - 1, rect.size.width, 1));}

2.重寫cell的setFrame方法,高度-1,露出背景色

- (void)setFrame:(CGRect)frame{ frame.size.height -= 1; // 給cellframe賦值 [super setFrame:frame];}

取消系統的分割線

設置tableView背景色為分割線顏色

// 取消系統分割線self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 設置tableView背景色self.tableView.backgroundColor = [UIColor colorWithWhite:215 / 255.0 alpha:1];

3.利用系統屬性設置(separatorInset, layoutMargins)共需添加三句代碼:

對tableView的separatorInset, layoutMargins屬性的設置

-(void)viewDidLoad { [super viewDidLoad]; //1.調整(iOS7以上)表格分隔線邊距 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { self.tableView.separatorInset = UIEdgeInsetsZero; } //2.調整(iOS8以上)view邊距(或者在cell中設置preservesSuperviewLayoutMargins,二者等效) if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { self.tableView.layoutMargins = UIEdgeInsetsZero; }}

對cell的LayoutMargins屬性的設置

對cell的設置可以寫在cellForRowAtIndexPath里,也可以寫在willDisplayCell方法里

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"cell"; FSDiscoverSpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[FSDiscoverSpecialCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //2.調整(iOS8以上)tableView邊距(與上面第2步等效,二選一即可) if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { cell.preservesSuperviewLayoutMargins = NO; } //3.調整(iOS8以上)view邊距 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } return cell;}

三種方法優缺點比較:

方法1是比較好用的,但是有些情況下系統自帶的cell就足夠用了,僅僅為了分隔線卻還必須再自定義cell,添加一個view,設置背景顏色和frame,又顯得麻煩;

方法2比較取巧,但是也需要自定義cell,在某些情況下不允許改變tableView的背景色,使用場景有限;

方法3不需要自定義cell,對系統(iOS7,iOS8以上)做個簡單判斷即可.可惜網上很多文章寫的不對,很多人不會正確使用,有些會用的人也說不清楚原理,只管復制粘貼.

比如網上流傳的一般是這樣,需要四步,雖然真的管用,但多了一步[cell setSeparatorInset:UIEdgeInsetsZero];而且原理也沒講,估計是某大神寫的,根本不屑于過多解釋,讓我用起來很郁悶,網上流傳代碼:

首先在viewDidLoad方法中加上如下代碼:

-(void)viewDidLoad { [super viewDidLoad]; if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsZero];}

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

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; }}

其實關于分隔線不能全屏的原理,蘋果官方在文件中已經說明了,可以去看一下

在iOS7之前系統默認就是全屏的,iOS7時UITableView多了separatorInset屬性,可在UITableView的頭文件中查看,如下:

@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators

iOS7時只要設置該屬性為UIEdgeInsetsZero就沒有問題了.

iOS8之后僅僅完成以上設置就不行了,仔細查看后發現iOS8的UIView的頭文件里又多了個layoutMargins屬性,并有官方注釋

@property (nonatomic) UIEdgeInsets layoutMargins NS_AVAILABLE_IOS(8_0);

/*
 -layoutMargins returns a set of insets from the edge of the view's bounds that denote a default spacing for laying out content.
 If preservesSuperviewLayoutMargins is YES, margins cascade down the view tree, adjusting for geometry offsets, so that setting the left value of layoutMargins on a superview will affect the left value of layoutMargins for subviews positioned close to the left edge of their superview's bounds
 If your view subclass uses layoutMargins in its layout or drawing, override -layoutMarginsDidChange in order to refresh your view if the margins change.
 */

大意是說:layoutMargins是view的bounds的邊距,用來調整內容默認邊距

如果preservesSuperviewLayoutMargins屬性是YES,那么設置父控件的layoutMargins邊距,就會影響所有子控件的相對于父控件bounds的layoutMargins邊距

如果你的view的子類在布局或者繪圖中使用了layoutMargins屬性,需要重寫-layoutMarginsDidChange 方法,以便當邊距改變時能刷新你的view

正是因為layoutMargins是UIView的新增屬性,tablet和cell作為UIView的子類都有這個屬性,所以相比較iOS7系統,iOS8之后就多了兩步,必須同時再對tableView和cell的layoutMargins屬性進行處理,才能讓分隔線真正全屏.

同時官方注釋中對preservesSuperviewLayoutMargins(意即:維持父控件的布局邊距)屬性的說明,也正好能說明網上另一種方法不設置self.tableView.layoutMargins = UIEdgeInsetsZero;而是設置cell.preservesSuperviewLayoutMargins = NO;為什么也能起作用

弄清楚了這些原理,就可以更好的記憶和使用這些方法,不用每次都去舊代碼查找或者去百度了.

說到了最后,不知道大家有沒有覺得影響分隔線全屏的元兇layoutMargins屬性 稍微有點眼熟呢?其實它在另一個地方也做了不少惡,就在storyboard中:


PS:附效果圖如下:

設置之前效果圖:

設置完第1步self.tableView.separatorInset = UIEdgeInsetsZero;后效果圖:

設置完第2步self.tableView.layoutMargins = UIEdgeInsetsZero;后效果圖:

設置完第3步cell.layoutMargins = UIEdgeInsetsZero;后效果圖:

附:設置UITableView的單元格分割線離屏幕左右的距離為0

在開發中,有時候為了界面的美觀,需要表示圖的分割線左右間距為0,即呈現下面的效果

有時候就直接取消顯示表視圖的分割線,然后在單元格內直接添加一條直線,這樣也能滿足要求,還有一種方法是改變表視圖內部的分割線的偏移量來實現,具體代碼如下:

if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {     [_tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];   }      if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {     [_tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];   } 
//代理方法 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {   if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {     [cell setSeparatorInset:UIEdgeInsetsZero];   }      if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {     [cell setLayoutMargins:UIEdgeInsetsZero];   } } 

這樣,就實現了單元格分割線的滿格顯示了。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 边坝县| 洱源县| 星子县| 罗江县| 增城市| 永新县| 安徽省| 江北区| 衡东县| 牙克石市| 柳林县| 霍州市| 高淳县| 苏尼特左旗| 大冶市| 鸡东县| 巨鹿县| 彭阳县| 贵定县| 尉氏县| 通海县| 柘荣县| 宁晋县| 沛县| 连云港市| 玉田县| 揭阳市| 泸定县| 岳普湖县| 休宁县| 垫江县| 大悟县| 怀柔区| 周宁县| 板桥市| 西宁市| 江山市| 靖西县| 司法| 鹤岗市| 焦作市|