tableview分割线

本文介绍如何在iOS应用中调整UITableView的分割线样式及位置,包括调整分隔线边距、使其从屏幕边界开始显示的方法。提供了多种实现方式,如通过调整separatorInset和layoutMargins属性来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默认分割线,左边不到屏幕;

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个像素即可实现;

 

转载于:https://www.cnblogs.com/OIMM/p/8989299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值