tableview分割線

默認分割線,左邊不到屏幕;

TableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

三種結構體樣式: 
/** 
UITableViewCellSeparatorStyleNone, 沒有分割線 
UITableViewCellSeparatorStyleSingleLine, 單線(默認) (左邊不到屏幕)
UITableViewCellSeparatorStyleSingleLineEtched 內嵌線 (左邊到屏幕)
*/


分割線從邊界開始方法一:
- (void)viewDidLoad {
    [super viewDidLoad];
   ...
   ...
   ...
#pragma mark - a 調整view邊距
    // 1.調整(iOS7以上)表格分隔線邊距
    if ([self.MyTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        self.MyTableView.separatorInset = UIEdgeInsetsZero;
    }
    // 2.調整(iOS8以上)view邊距(或者在cell中設置preservesSuperviewLayoutMargins,兩者等效)
    if ([self.MyTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        self.MyTableView.layoutMargins = UIEdgeInsetsZero;
    }

}

#pragma mark - b 調整view邊距
//而後在willDisplayCell方法中加入以下代碼:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
#pragma mark - b
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

方法二:spa

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
...
...
#pragma mark - a 調整view邊距
    //1.調整(iOS8以上)tableView邊距(與上面第2步等效,二選一便可)
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        cell.preservesSuperviewLayoutMargins = NO;
    }
    //2.調整(iOS8以上)view邊距
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    return cell;
}

#pragma mark - b 調整view邊距
//而後在willDisplayCell方法中加入以下代碼:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
#pragma mark - b
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

方法三:code

系統自帶的cell的分割線,知足咱們大部分的需求,但在有些狀況下,咱們須要使用樣式二中得cell的分割線樣式。 
同時,咱們也能夠自定義cell的分割線。經過1個像素寬的圖片或者view添加到cell中; 
或者設置背景圖片爲灰色,同時設置cell之間的間距爲1個像素便可實現;blog

相關文章
相關標籤/搜索