一般TableView設(shè)置全屏分隔線有下面三種方法
1.自定義cell,手動(dòng)添加分割線
隱藏自帶的
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
可以通過(guò)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.重寫(xiě)cell的setFrame方法,高度-1,露出背景色
- (void)setFrame:(CGRect)frame
{
frame.size.height -= 1;
// 給cellframe賦值
[super setFrame:frame];
}
- 取消系統(tǒng)的分割線
- 設(shè)置tableView背景色為分割線顏色
// 取消系統(tǒng)分割線
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 設(shè)置tableView背景色
self.tableView.backgroundColor = [UIColor colorWithWhite:215 / 255.0 alpha:1];
3.利用系統(tǒng)屬性設(shè)置(separatorInset, layoutMargins)共需添加三句代碼:
- 對(duì)tableView的separatorInset, layoutMargins屬性的設(shè)置
-(void)viewDidLoad {
[super viewDidLoad];
//1.調(diào)整(iOS7以上)表格分隔線邊距
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
self.tableView.separatorInset = UIEdgeInsetsZero;
}
//2.調(diào)整(iOS8以上)view邊距(或者在cell中設(shè)置preservesSuperviewLayoutMargins,二者等效)
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
}
- 對(duì)cell的LayoutMargins屬性的設(shè)置
對(duì)cell的設(shè)置可以寫(xiě)在cellForRowAtIndexPath里,也可以寫(xiě)在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.調(diào)整(iOS8以上)tableView邊距(與上面第2步等效,二選一即可)
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
cell.preservesSuperviewLayoutMargins = NO;
}
//3.調(diào)整(iOS8以上)view邊距
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
return cell;
}
三種方法優(yōu)缺點(diǎn)比較:
方法1是比較好用的,但是有些情況下系統(tǒng)自帶的cell就足夠用了,僅僅為了分隔線卻還必須再自定義cell,添加一個(gè)view,設(shè)置背景顏色和frame,又顯得麻煩;
方法2比較取巧,但是也需要自定義cell,在某些情況下不允許改變tableView的背景色,使用場(chǎng)景有限;
方法3不需要自定義cell,對(duì)系統(tǒng)(iOS7,iOS8以上)做個(gè)簡(jiǎn)單判斷即可.可惜網(wǎng)上很多文章寫(xiě)的不對(duì),很多人不會(huì)正確使用,有些會(huì)用的人也說(shuō)不清楚原理,只管復(fù)制粘貼.
比如網(wǎng)上流傳的一般是這樣,需要四步,雖然真的管用,但多了一步[cell setSeparatorInset:UIEdgeInsetsZero];
而且原理也沒(méi)講,估計(jì)是某大神寫(xiě)的,根本不屑于過(guò)多解釋,讓我用起來(lái)很郁悶,網(wǎng)上流傳代碼:
首先在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];
}
}
其實(shí)關(guān)于分隔線不能全屏的原理,蘋果官方在文件中已經(jīng)說(shuō)明了,可以去看一下
在iOS7之前系統(tǒng)默認(rèn)就是全屏的,iOS7時(shí)UITableView
多了separatorInset
屬性,可在UITableView
的頭文件中查看,如下:
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
// allows customization of the frame of cell separators
iOS7時(shí)只要設(shè)置該屬性為UIEdgeInsetsZero
就沒(méi)有問(wèn)題了.
iOS8之后僅僅完成以上設(shè)置就不行了,仔細(xì)查看后發(fā)現(xiàn)iOS8的UIView
的頭文件里又多了個(gè)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.
*/
大意是說(shuō):layoutMargins是view的bounds的邊距,用來(lái)調(diào)整內(nèi)容默認(rèn)邊距
如果preservesSuperviewLayoutMargins屬性是YES,那么設(shè)置父控件的layoutMargins邊距,
就會(huì)影響所有子控件的相對(duì)于父控件bounds的layoutMargins邊距
如果你的view的子類在布局或者繪圖中使用了layoutMargins屬性,需要重寫(xiě)-layoutMarginsDidChange 方法,
以便當(dāng)邊距改變時(shí)能刷新你的view
正是因?yàn)閘ayoutMargins是UIView的新增屬性,tablet和cell作為UIView的子類都有這個(gè)屬性,所以相比較iOS7系統(tǒng),iOS8之后就多了兩步,必須同時(shí)再對(duì)tableView和cell的layoutMargins屬性進(jìn)行處理,才能讓分隔線真正全屏.
同時(shí)官方注釋中對(duì)preservesSuperviewLayoutMargins(意即:維持父控件的布局邊距)屬性的說(shuō)明,也正好能說(shuō)明網(wǎng)上另一種方法不設(shè)置self.tableView.layoutMargins = UIEdgeInsetsZero;
而是設(shè)置cell.preservesSuperviewLayoutMargins = NO;
為什么也能起作用
弄清楚了這些原理,就可以更好的記憶和使用這些方法,不用每次都去舊代碼查找或者去百度了.
說(shuō)到了最后,不知道大家有沒(méi)有覺(jué)得影響分隔線全屏的元兇layoutMargins屬性 稍微有點(diǎn)眼熟呢?其實(shí)它在另一個(gè)地方也做了不少惡,就在storyboard中:
PS:附效果圖如下:
設(shè)置之前效果圖:
設(shè)置完第1步
self.tableView.separatorInset = UIEdgeInsetsZero;
后效果圖:設(shè)置完第2步
self.tableView.layoutMargins = UIEdgeInsetsZero;
后效果圖:設(shè)置完第3步
cell.layoutMargins = UIEdgeInsetsZero;
后效果圖: