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

相关文章
相关标签/搜索