csdnceshi62
2014-09-10 16:05
采纳率: 100%
浏览 520
已采纳

8 UITableView 分隔符0没有工作

I have an app where the UITableView's separator inset is set to custom values - Right 0, Left 0. This works perfectly in iOS 7.x, however in iOS 8.0 I see that the separator inset is set to the default of 15 on the right. Even though in the xib files it set to 0, it still shows up incorrectly.

How do I remove the UITableViewCell separator margins?

转载于:https://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

29条回答 默认 最新

  • local-host 2014-09-16 20:19
    已采纳

    iOS 8.0 introduces the layoutMargins property on cells AND table views.

    This property isn't available on iOS 7.0 so you need to make sure you check before assigning it!

    Additionally, Apple has added a property to your cell that will prevent it from inheriting your Table View's margin settings. When this property is set, your cells are allowed to configure their own margins independently of the table view. Think of it as an override.

    This property is called preservesSuperviewLayoutMargins, and setting it to NO will allow the cell's layoutMargin setting to override whatever layoutMargin is set on your TableView. It both saves time (you don't have to modify the Table View's settings), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.

    NOTE: what follows is a clean implementation for a cell-level margin setting, as expressed in Mike Abdullah's answer. Setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings. If you actually want your entire table view to have consistent margins, please adjust your code accordingly.

    Setup your cell margins:

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Remove seperator inset
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
               [cell setSeparatorInset:UIEdgeInsetsZero];
        }
    
        // Prevent the cell from inheriting the Table View's margin settings
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
    
        // Explictly set your cell's layout margins
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    

    Swift 4:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // Remove seperator inset
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = .zero
        }
        // Prevent the cell from inheriting the Table View's margin settings
        if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false
        }
        // Explictly set your cell's layout margins
        if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
            cell.layoutMargins = .zero
        }
    }
    

    Setting the preservesSuperviewLayoutMargins property on your cell to NO should prevent your table view from overriding your cell margins. In some cases, it seems to not function properly.

    If all fails, you may brute-force your Table View margins:

    -(void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
    
        // Force your tableview margins (this may be a bad idea)
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        }
    
        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        }
    } 
    

    Swift 4:

    func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        // Force your tableview margins (this may be a bad idea)
        if tableView.responds(to: #selector(setter: UITableView.separatorInset)) {
            tableView.separatorInset = .zero
        }
        if tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {
            tableView.layoutMargins = .zero
        }
    }
    

    ...and there you go! This should work on iOS 7 and 8.

    EDIT: Mohamed Saleh brought to my attention a possible change in iOS 9. You may need to set the Table View's cellLayoutMarginsFollowReadableWidth to NO if you want to customize insets or margins. Your mileage may vary, this is not documented very well.

    This property only exists in iOS 9 so be sure to check before setting.

    if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
    {
        myTableView.cellLayoutMarginsFollowReadableWidth = NO;
    } 
    

    Swift 4:

    if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {
        myTableView.cellLayoutMarginsFollowReadableWidth = false
    }
    

    (above code from iOS 8 UITableView separator inset 0 not working)

    EDIT: Here's a pure Interface Builder approach:

    TableViewAttributesInspector TableViewCellSizeInspector

    NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...

    点赞 评论
  • 乱世@小熊 2014-09-10 18:02

    Arg!!! After playing around either doing this in your Cell subclass:

    - (UIEdgeInsets)layoutMargins
    {
        return UIEdgeInsetsZero;
    }
    

    or setting the cell.layoutMargins = UIEdgeInsetsZero; fixed it for me.

    点赞 评论
  • MAO-EYE 2014-09-10 21:35

    In Swift it's slightly more annoying because layoutMargins is a property, so you have to override the getter and setter.

    override var layoutMargins: UIEdgeInsets {
      get { return UIEdgeInsetsZero }
      set(newVal) {}
    }
    

    This will effectively make layoutMargins readonly, which in my case is fine.

    点赞 评论
  • Memor.の 2014-09-11 12:54

    I believe this is the same question that I asked here: Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator

    In iOS 8, there is one new property for all the objects inherit from UIView. So, the solution to set the SeparatorInset in iOS 7.x will not be able to remove the white space you see on the UITableView in iOS 8.

    The new property is called "layoutMargins".

    @property(nonatomic) UIEdgeInsets layoutMargins
    Description   The default spacing to use when laying out content in the view.
    Availability  iOS (8.0 and later)
    Declared In   UIView.h
    Reference UIView Class Reference
    

    iOS 8 UITableView setSeparatorInset:UIEdgeInsetsZero setLayoutMargins:UIEdgeInsetsZero

    The solution:-

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

    If you set cell.layoutMargins = UIEdgeInsetsZero; without checking if the layoutMargins exists, the app will crash on iOS 7.x. So, the best way would be checking if the layoutMargins exists first before setLayoutMargins:UIEdgeInsetsZero.

    点赞 评论
  • 谁还没个明天 2014-09-26 10:22

    As to what cdstamper suggested instead of the table view, adding below lines in the cell's layoutSubview method works for me.

    - (void)layoutSubviews 
    {
        [super layoutSubviews];
    
        if ([self respondsToSelector:@selector(setSeparatorInset:)])
                    [self setSeparatorInset:UIEdgeInsetsZero];
    
            if ([self respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
            {
                [self setPreservesSuperviewLayoutMargins:NO];;
            }
    
            if ([self respondsToSelector:@selector(setLayoutMargins:)]) 
            {
                [self setLayoutMargins:UIEdgeInsetsZero];
            }
    }
    
    点赞 评论
  • 程序go 2014-10-21 23:35

    Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        if self.tableView.respondsToSelector("setSeparatorInset:") {
            self.tableView.separatorInset = UIEdgeInsetsZero
        }
        if self.tableView.respondsToSelector("setLayoutMargins:") {
            self.tableView.layoutMargins = UIEdgeInsetsZero
        }
    
        self.tableView.layoutIfNeeded()            // <--- this do the magic
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         ...
    
        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset = UIEdgeInsetsZero
        }
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins = UIEdgeInsetsZero
        }
    
        return cell
    }
    
    点赞 评论
  • 7*4 2014-10-29 16:58

    Most answers are showing separator insets and layout margins being set over a variety of methods (i.e., viewDidLayoutSubviews, willDisplayCell, etc) for cells and tableviews, but I've found that just putting these in cellForRowAtIndexPath works great. Seems like the cleanest way.

    // kill insets for iOS 8
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
        cell.preservesSuperviewLayoutMargins = NO;
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    // iOS 7 and later
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
        [cell setSeparatorInset:UIEdgeInsetsZero];
    
    点赞 评论
  • 七度&光 2014-11-03 12:24

    Use below code snippet avoid unwanted padding issue for UITableView in IOS 8 & 7.

    -(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];
        }
    }
    
    点赞 评论
  • ?yb? 2014-11-03 16:26

    This is the code that's working for me, in Swift:

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

    This seems the cleanest to me (for now), as all the cell/tableView edge/margin adjustments are done in the tableView:willDisplayCell:forRowAtIndexPath: method, without cramming unneccessary code into tableView:cellForRowAtIndexPath:.

    Btw, I'm only setting the cell's left separatorInset/layoutMargins, because in this case I don't want to screw up my constraints that I have set up in my cell.

    Code updated to Swift 2.2 :

     override func viewDidLoad() {
       super.viewDidLoad()       
    
        if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
          tableView.separatorInset = UIEdgeInsetsZero
            }
        }
    
     override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
            if cell.respondsToSelector(Selector("setSeparatorInset:")) {
                cell.separatorInset.left = CGFloat(0.0)
            }
            if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
                tableView.layoutMargins = UIEdgeInsetsZero
            }
            if cell.respondsToSelector(Selector("setLayoutMargins:")) {
                cell.layoutMargins.left = CGFloat(0.0)
            }
        }
    
    点赞 评论
  • python小菜 2014-11-09 10:49

    Let's take a moment to understand the problem before blindly charging in to attempt to fix it.

    A quick poke around in the debugger will tell you that separator lines are subviews of UITableViewCell. It seems that the cell itself takes a fair amount of responsibility for the layout of these lines.

    iOS 8 introduces the concept of layout margins. By default, a view's layout margins are 8pt on all sides, and they're inherited from ancestor views.

    As best we can tell, when laying out out its separator line, UITableViewCell chooses to respect the left-hand layout margin, using it to constrain the left inset.

    Putting all that together, to achieve the desired inset of truly zero, we need to:

    • Set the left layout margin to 0
    • Stop any inherited margins overriding that

    Put like that, it's a pretty simple task to achieve:

    cell.layoutMargins = UIEdgeInsetsZero;
    cell.preservesSuperviewLayoutMargins = NO;
    

    Things to note:

    • This code only needs to be run once per cell (you're just configuring the cell's properties after all), and there's nothing special about when you choose to execute it. Do what seems cleanest to you.
    • Sadly neither property is available to configure in Interface Builder, but you can specify a user-defined runtime attribute for preservesSuperviewLayoutMargins if desired.
    • Clearly, if your app targets earlier OS releases too, you'll need to avoid executing the above code until running on iOS 8 and above.
    • Rather than setting preservesSuperviewLayoutMargins, you can configure ancestor views (such as the table) to have 0 left margin too, but this seems inherently more error-prone as you don't control that entire hierarchy.
    • It would probably be slightly cleaner to set only the left margin to 0 and leave the others be.
    • If you want to have a 0 inset on the "extra" separators that UITableView draws at the bottom of plain style tables, I'm guessing that will require specifying the same settings at the table level too (haven't tried this one!)
    点赞 评论
  • 旧行李 2014-12-01 10:23

    Instead of updating preservesSuperviewLayoutMargins and layoutMargins every time the cell scrolls in (using willDisplayCell), I'd suggest to do it once in cellForRowAtIndexPath::

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
    
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
    
        return cell
    
    }
    
    点赞 评论
  • csdnceshi62 2014-12-23 18:34

    Swift version

    iOS introduces the layoutMargins property on cells AND table views.

    This property isn't available in iOS 7.0 so you need to make sure you check before assigning it!

    However, Apple has added a property called preservesSuperviewLayoutMargins to your cell that will prevent it from inheriting your Table View's margin settings. This way, your cells can configure their own margins independently of the table view. Think of it as an override.

    This property is called preservesSuperviewLayoutMargins, and setting it to NO can allow you to override your Table View's layoutMargin settings with your own cell's layoutMargin setting. It both saves time (you don't have to modify the Table View's settings), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.

    NOTE: this is the proper, less messy implementation, as expressed in Mike Abdullah's answer; setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings.

    First step - Setup your cell margins:

    /*
        Tells the delegate that the table view is about to draw a cell for a particular row.
    */
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
        forRowAtIndexPath indexPath: NSIndexPath)
    {
        // Remove separator inset
        if cell.respondsToSelector("setSeparatorInset:") {
            cell.separatorInset = UIEdgeInsetsZero
        }
    
        // Prevent the cell from inheriting the Table View's margin settings
        if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
            cell.preservesSuperviewLayoutMargins = false
        }
    
        // Explictly set your cell's layout margins
        if cell.respondsToSelector("setLayoutMargins:") {
            cell.layoutMargins = UIEdgeInsetsZero
        }
    }
    

    Setting the preservesSuperviewLayoutMargins property on your cell to NO should prevent your table view from overriding your cell margins. In some cases, it seems not to function properly.

    Second step - Only if all fails, you may brute-force your Table View margins:

    /*
        Called to notify the view controller that its view has just laid out its subviews.
    */
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        // Force your tableview margins (this may be a bad idea)
        if self.tableView.respondsToSelector("setSeparatorInset:") {
            self.tableView.separatorInset = UIEdgeInsetsZero
        }
    
        if self.tableView.respondsToSelector("setLayoutMargins:") {
            self.tableView.layoutMargins = UIEdgeInsetsZero
        }
    }
    

    ...and there you go! This should work on iOS 8 as well as iOS 7.

    Note: tested using iOS 8.1 and 7.1, in my case I only needed to use the first step of this explanation.

    The Second Step is only required if you have unpopulated cell beneath the rendered cells, ie. if the table is larger than the number of rows in the table model. Not doing the second step would result in different separator offsets.

    点赞 评论
  • Didn"t forge 2014-12-30 10:19

    I made it work by doing this:

    tableView.separatorInset = UIEdgeInsetsZero;
    tableView.layoutMargins = UIEdgeInsetsZero;
    cell.layoutMargins = UIEdgeInsetsZero;
    
    点赞 评论
  • 狐狸.fox 2015-02-03 10:09

    You can use UIAppearance once, at your application startup (before UI is loaded), to set it as default global settings:

    // iOS 7:
    [[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
    
    [[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];
    
    // iOS 8:
    if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
    
        [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
        [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
        [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
    
    }
    

    This way, you keep your UIViewController's code clean and can always override it if you want.

    点赞 评论
  • 旧行李 2015-04-20 20:47

    Lukasz answer in Swift:

        // iOS 7:
        UITableView.appearance().separatorStyle = .SingleLine
        UITableView.appearance().separatorInset = UIEdgeInsetsZero
        UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero
    
        // iOS 8:
        if UITableView.instancesRespondToSelector("setLayoutMargins:") {
            UITableView.appearance().layoutMargins = UIEdgeInsetsZero
            UITableViewCell.appearance().layoutMargins = UIEdgeInsetsZero
            UITableViewCell.appearance().preservesSuperviewLayoutMargins = false
        }
    
    点赞 评论
  • 衫裤跑路 2015-06-04 09:24

    Simple solution in Swift for iOS 8 with a custom UITableViewCell

    override func awakeFromNib() {
        super.awakeFromNib()
    
        self.layoutMargins = UIEdgeInsetsZero
        self.separatorInset = UIEdgeInsetsZero
    }
    

    In this way you are setting layoutMargin and separatorInset just one time instead of doing it for each willDisplayCell as most of the above answers suggest.

    If you are using a custom UITableViewCell this is the correct place to do it. Otherwise you should do it in tableView:cellForRowAtIndexPath.

    Just another hint: you don't need to set preservesSuperviewLayoutMargins = false because default value is already NO!

    点赞 评论
  • 撒拉嘿哟木头 2015-06-09 13:55

    In a more compact way than the most voted answer...

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

    }

    点赞 评论
  • 谁还没个明天 2015-06-10 11:18

    In iOS8:

    Adding this to my UITableViewCell Subclass:

    - (UIEdgeInsets)layoutMargins {
        return UIEdgeInsetsZero;
    }
    

    and this to "tableView:cellForRowAtIndexPath" or "tableView:willDisplayCell":

    [editCell setSeparatorInset:UIEdgeInsetsZero];
    

    WORKED for me.

    点赞 评论
  • 喵-见缝插针 2015-08-29 10:05

    After having seen the answers at floor 3, I tried to figure out what the relationship of setting up the separator between TableView & TableViewCell and did some test. Here are my conclusions:

    1. we can consider that setting the cell's separator to zero has to move the separator in two steps: first step is to set cell's separatorinset to zero. second step is to set cell's marginlayout to zero.

    2. set the TableView's separatorinset and marginlayout can affect the Cell's separatorinset. However, from the test, I find that the TableView's separatorinset seem to be useless, TableView's marginlayout can actually affect cell's marginlayout.

    3. set Cell's PreservesSuperviewLayoutMargins = false, can cut off TableView's marginlayout effect on Cells.

    4. one of the solutions:

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
          var cell = UITableViewCell()
      
          cell.preservesSuperviewLayoutMargins = false
          cell.separatorInset = UIEdgeInsetsZero
          cell.layoutMargins = UIEdgeInsetsZero
      
          return cell
      }
      
    点赞 评论
  • 零零乙 2015-09-02 14:52

    Adding this snippet, simple elegant in Swift works for me in iOS8 :)

        // tableview single line
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
    }
    
    点赞 评论
  • from.. 2015-09-09 07:00

    This is my solution. This applies to the custom cell subclass, just add them both to the subclass.

    1. - (UIEdgeInsets)layoutMargins {    
          return UIEdgeInsetsMake(0, 10, 0, 10);
      }
      

    2.

    self.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    

    And it is convenient that you can customize the position of the separator without asking your designer to draw one for you..........

    点赞 评论
  • 七度&光 2015-09-09 16:55

    Swift 2.0 Extension

    I just wanted to share an extension I made to remove the margins from the tableview cell separators.

    extension UITableViewCell {
        func removeMargins() {
    
            if self.respondsToSelector("setSeparatorInset:") {
                self.separatorInset = UIEdgeInsetsZero
            }
    
            if self.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
                self.preservesSuperviewLayoutMargins = false
            }
    
            if self.respondsToSelector("setLayoutMargins:") {
                self.layoutMargins = UIEdgeInsetsZero
            }
        }
    }
    

    Used in context:

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
    
        cell.removeMargins()
        return cell
    
    点赞 评论
  • 胖鸭 2015-09-30 07:41

    For iOS 9 you need to add:

    if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
    {
        myTableView.cellLayoutMarginsFollowReadableWidth = NO;
    } 
    

    For more details please refer to question.

    点赞 评论
  • 游.程 2015-10-03 11:24

    This worked perfectly for me in iOS 8 and iOS 9.

    For OBJ-C

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
            }
             return cell;
        }
    
    点赞 评论
  • 10.24 2015-10-23 08:29

    Here's an easy way to globally remove the inset.

    In UITableViewCell+Extensions.swift:

    import UIKit
    
    extension UITableViewCell {
    
      override public var layoutMargins: UIEdgeInsets {
        get { return UIEdgeInsetsZero }
        set { }
      }
    
    }
    

    In AppDelegate application:didFinishLaunchingWithOptions::

      UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero
    

    You might think to a) also just override separatorInset in the extension, or b) set the appearance proxy for layoutMargins, instead. Neither will work. Even though separatorInset is indicated to be a property, attempting to override it as a property (or method) generates compiler errors. And setting the appearance proxy for UITableViewCell's layoutMargins (or, for that matter, also setting the appearance proxies for UITableView's layoutMargins and separatorInset) has no effect.

    点赞 评论
  • larry*wei 2016-02-18 10:30

    For me the simple line did the job

    cell.layoutMargins = UIEdgeInsetsZero
    
    点赞 评论
  • csdnceshi62 2016-07-05 14:51

    After much investigation...

    Here's the only way to fully control this stuff (that I could find)

    To fully control both separator insets and layout margins on each cell. Do this in the willDisplayCell method on your UITableviewDelegate.

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.layoutMargins = UIEdgeInsetsZero
        cell.contentView.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10)
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
    }
    

    The cell object controls the separator, and the contentView controls everything else. If your separator inset spaces are showing up in an unexpected color this should solve it:

    cell.backgroundColor = cell.contentView.backgroundColor
    
    点赞 评论
  • from.. 2016-07-15 03:13

    Just add below code can solve this program.

    Good luck to you!

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
           if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
               [cell setSeparatorInset:UIEdgeInsetsZero];
           }
    
           if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
               [cell setLayoutMargins:UIEdgeInsetsZero];
           }
    
    }
    
    点赞 评论
  • 10.24 2016-08-17 20:32

    Swift 3.0 example:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // removing seperator inset
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = .zero
        }
        // prevent the cell from inheriting the tableView's margin settings
        if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false
        }
        // explicitly setting cell's layout margins
        if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
            cell.layoutMargins = .zero
        }
    }
    
    点赞 评论

相关推荐 更多相似问题