在使用tableView的時(shí)候發(fā)現(xiàn), cell分割線右邊有一點(diǎn)缺失.總結(jié)三個(gè)補(bǔ)全方法,使cell的分割線補(bǔ)全:
1 添加一個(gè)自動(dòng)以view
實(shí)現(xiàn)步驟:
- 取消系統(tǒng)的分隔線
- 自定義cell, 在layOutsubviews添加自定義分隔線
#import "ViewController.h"
#import "JNCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 取消系統(tǒng)的分隔線
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pargma mark --自定義cell
#import "JNCell.h"
@implementation JNell
- (void)layoutSubviews {
[super layoutSubviews];
// 初始化數(shù)據(jù)
CGFloat w = self.frame.size.width;
CGFloat h = 1;
CGFloat x = 0;
CGFloat y = self.frame.size.height - 1;
// 添加自定義分隔線
UIView *sepLine = [[UIView alloc] init];
sepLine.frame = CGRectMake(x, y, w, h);
// 設(shè)置背景色
sepLine.backgroundColor = [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1];
[self.contentView addSubview:sepLine];
}
@end
2 在TableView中所在的方法中添加相應(yīng)的方法
注意 :
該方法在iOS7.0系統(tǒng)及以下會(huì)報(bào)錯(cuò);因?yàn)閘ayoutMargins屬性是8.0才有的
- (void)viewDidLayoutSubviews {
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
self.tableView.separatorInset = UIEdgeInsetsZero;
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
cell.separatorInset = UIEdgeInsetsZero;
}
}
3 通過(guò)背景顏色偷梁換柱
實(shí)現(xiàn)方法:
- 取消系統(tǒng)的分隔線
- 設(shè)置tableView的背景顏色
- 自定義cell, 重寫(xiě)系統(tǒng)的setFrame:方法
#import "ViewController.h"
#import "JNCell.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.0 取消系統(tǒng)的分隔線
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 2.0 設(shè)置背景顏色
self.tableView.backgroundColor = [UIColor grayColor];
}
#pargram mark -- 自定義cell
#import "JNCell.h"
@implementation ABCell
// 重寫(xiě)系統(tǒng)方法
- (void)setFrame:(CGRect)frame {
// 設(shè)置分隔線的高度
frame.size.height -= 1;
// 調(diào)用系統(tǒng)的方法
[super setFrame:frame];
}
/**
當(dāng)系統(tǒng)自動(dòng)計(jì)算好cell的尺寸后, 會(huì)調(diào)用該方法給cell設(shè)置frame
這時(shí)候重寫(xiě)setframe方法, 把cell的高度減1; 故顯示后會(huì)露出高度為1(tableView的)背景顏色 (充當(dāng)分隔線)
*/