如何使UITableView分隔符插入从屏幕的边缘?

class RoomNewTableViewCell: UITableViewCell { var row:Int? var room: Room?{ didSet{ updateUI() //this is when render is called } } override func awakeFromNib() { super.awakeFromNib() self.layoutMargins = UIEdgeInsetsZero self.separatorInset = UIEdgeInsetsMake(0, -15, 0, 0) } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func updateUI(){ println("Updating UI") let test = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) test.backgroundColor = UIColor.greenColor() self.backgroundColor = UIColor.blueColor() self.addSubview(test) } } 

}

这是我如何创build我的tableView:

  //Draw Table self.roomsTableView.delegate = self self.roomsTableView.dataSource = self self.roomsTableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine self.roomsTableView.separatorColor = UIColor(hex: 0xededed) let tableViewStartY = CGFloat(0) //used to be 20.0 let extraPaddingToAlignTable = CGFloat(50) self.roomsTableView.frame = CGRectMake(0, tableViewStartY, screenWidth, CGRectGetMinY(self.tabBarController!.tabBar.frame) + extraPaddingToAlignTable) self.roomsTableView.addSubview(self.refreshControl) self.roomsTableView.tableFooterView = UIView(frame: CGRectZero) self.roomsTableView.rowHeight = CGFloat(100) self.roomsTableView.clipsToBounds = true self.roomsTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0) self.roomsTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) self.roomsTableView.registerClass(RoomNewTableViewCell.self, forCellReuseIdentifier: "newCell") self.view.addSubview(self.roomsTableView) 

我已经尝试在表格和单元格周围使用separatorInset,但似乎无法使其工作。

在这里输入图像说明

自iOS 8.0发布以来,苹果为UITableViewCell和UITableView添加了layoutMargins属性。

所以你可以将layoutMargin设置为零,在willDisplayCell: TableView的委托方法。 这段代码只是将tableview的layoutMargin和cell layoutMarginUIEdgeInsetsZero

这也适用于iOS 7.0。

尝试这可能会帮助你。

迅速

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if(tableView.respondsToSelector("setSeparatorInset:")){ tableView.separatorInset = UIEdgeInsetsZero } if(tableView.respondsToSelector("setLayoutMargins:")){ tableView.layoutMargins = UIEdgeInsetsZero } if(cell.respondsToSelector("setLayoutMargins:")){ cell.layoutMargins = UIEdgeInsetsZero } } 

目标C

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

对于iOS 9.0,您必须将cellLayoutMarginsFollowReadableWidth设置为NO

目标C

 - (void)viewDidLoad { [super viewDidLoad]; //For iOS 9 and Above if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) { self.tableView.cellLayoutMarginsFollowReadableWidth = NO; } } 

迅速

 override func viewDidLoad() { super.viewDidLoad() //For iOS 9 and Above if #available(iOS 9, *) { tableView.cellLayoutMarginsFollowReadableWidth = false } } 

您还可以从Storyboard中设置此值。

打开Storyboard并转到TableView Cell的Attribute Inspector。

将“ Separator字段的types更改为“ Custom Insets ,并将“ Left值”更改为0

请find下面的图片。

在这里输入图像说明