最新消息:iOS编程开发交流群(6906921) ,Mac.Cocoa开发交流群(7758675) 欢迎iOS/macOS开发编程爱好及学习者加入!

iOS7 tableview separatorInset cell分割线左对齐

iOS 天狐 23237浏览 1评论
在iOS7中,表格中经常看到的一个情况是如下所示,

解决方法:

1,手写代码控制

self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
但是在iOS7之前是没有这个设置,所以要加下判断。以免程序在iOS7之前的环境下运行崩溃。
 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }

 

2,nib,通过下图把Separator Insets的模式改为Custom,并且把Left的15改为0

 ===========================================
IOS8最新设置方法
-(void)viewDidLayoutSubviews {
    
    if ([self.mytableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.mytableview setSeparatorInset:UIEdgeInsetsZero];

    }
    if ([self.mytableview respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.mytableview setLayoutMargins:UIEdgeInsetsZero];
    }

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

方法2:

使用UIAppearance,在程序开始的时候设置全局默认外观,不错的方法

[[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
[[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];

if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
    [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
}

 

转载请注明:天狐博客 » iOS7 tableview separatorInset cell分割线左对齐

微信 OR 支付宝 扫描二维码
为天狐 打赏
非常感谢你的支持,哥会继续努力!
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 此方法在IOS8下不管用了,ios8需要这样处理: -(void)viewDidLayoutSubviews { [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)]; [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; } -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ [cell setLayoutMargins:UIEdgeInsetsZero]; [cell setSeparatorInset:UIEdgeInsetsZero]; }
    弗丁11年前 (2014-11-08)回复