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];
    }
}

方法二:

-(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];
    }
}

方法三:

系統自帶的cell的分割線,滿足我們大部分的需求,但在有些情況下,我們需要使用樣式二中得cell的分割線樣式。 
同時,我們也可以自定義cell的分割線。通過1個像素寬的圖片或者view添加到cell中; 
或者設置背景圖片為灰色,同時設置cell之間的間距為1個像素即可實現;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM