有没有办法使iOS 7中的UITableView单元格在分隔符中没有换行符?

有没有办法使iOS 7中的UITableView单元格在分隔符中没有换行符?,uitableview,ios7,Uitableview,Ios7,我注意到在iOS 7中,iTableViewCells在iOS 6中没有的单元格分隔符中有一个换行符。有没有办法摆脱这个断线?将分隔符更改为“无”,然后使用分隔符的颜色创建UIVIEW仍然会导致出现白色分隔符。解决了这个问题 [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; 对于iOS7: if ([self.tableView respondsToSelector:@selector(setSeparatorIn

我注意到在iOS 7中,iTableViewCells在iOS 6中没有的单元格分隔符中有一个换行符。有没有办法摆脱这个断线?将分隔符更改为“无”,然后使用分隔符的颜色创建UIVIEW仍然会导致出现白色分隔符。

解决了这个问题

[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];

对于iOS7:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
/*
    Tells the delegate 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 seperator 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
    }
}
/*
    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
    }
}
对于iOS8:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
/*
    Tells the delegate 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 seperator 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
    }
}
/*
    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
    }
}
首先按如下方式配置表视图:

if ([self.tableView respondsToSelector:@selector(layoutMargins)]) {
    self.tableView.layoutMargins = UIEdgeInsetsZero;
}
if ([cell respondsToSelector:@selector(layoutMargins)]) {
    cell.layoutMargins = UIEdgeInsetsZero;
}
然后在cellForRowAtIndexPath:方法中,按如下方式配置单元格:

if ([self.tableView respondsToSelector:@selector(layoutMargins)]) {
    self.tableView.layoutMargins = UIEdgeInsetsZero;
}
if ([cell respondsToSelector:@selector(layoutMargins)]) {
    cell.layoutMargins = UIEdgeInsetsZero;
}

注意:包括layoutMargins和separatorInset,要支持这两个iOS版本,您还可以从情节提要设置“分隔符插入:


如果您想支持较旧版本的iOS,请在调用该方法之前检查此方法的可用性:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}

选定的答案对我不起作用。但这是肯定的,它在ios 2.0上运行:

   [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

重置分离器为我解决了此问题:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:self.tableView.separatorInset];
}

对于应用程序范围内的永久性解决方案,请在应用程序中调用此选项:didFinishLaunching。。任何tableview都将被“修复”


注意:setSeparatorStyle行必须位于超级调用上方。

当心,因为将tableView的分隔符设置为UIEdgeInsetsZero似乎同时会打破章节标题的左边距!(至少在iOS 7.1上是这样)

如果要使用tableView:TitleForHeaderInstruction:显示节标题,并且仍然可以获得UITableViewCells之间不换行的理想效果,则必须直接在单元格上而不是在tableView上设置分隔符集

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    [cell setSeparatorInset:UIEdgeInsetsZero];

在iOS 8中,似乎有一个小bug。分隔符插入的自定义值不会应用于包含文本的单元格。我试着通过界面生成器和代码设置它,但都不起作用。我也提交了一份bug报告

同时,我有一个小的解决办法来实现这一点。我只是在牢房上画了条线。创建
UITableViewCell
的子类,并重写
drawRect
方法。另外,不要忘记将
UITableView
的separator属性设置为None。这是代码。它在Swift中,但由于它基本上是C,所以您也可以在Objective-C中使用相同的东西

override func drawRect(rect: CGRect) {
    super.drawRect(rect)

    let context = UIGraphicsGetCurrentContext()
    CGContextSetStrokeColorWithColor(context, UITableView().separatorColor.CGColor) // seperator color
    CGContextSetLineWidth(context, 2) // separator width

    CGContextMoveToPoint(context, 0, self.bounds.size.height)
    CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height)
    CGContextStrokePath(context)
}

在iOS 8中,基于Seth McFarlane邮报,你需要开始处理裁员利润。以下是为我做的:

  • 在ApplicationIDFinishLaunching中,运行以下操作:

    if ([UITableViewCell instancesRespondToSelector:@selector(setLayoutMargins:)]) {
        [[UITableViewCell appearance]setLayoutMargins:UIEdgeInsetsZero];
    }
    
    -(void) viewWillLayoutSubviews {
      [super viewWillLayoutSubviews];
      UITableView* tv = self.tableView;
      if ([tv respondsToSelector:@selector(setLayoutMargins:)]) {
        [tv setLayoutMargins:UIEdgeInsetsZero];
      }
    }
    
  • 在UITableView上创建以下类别:

    @interface UITableView(WJAdditions)
    -(void) wjZeroSeparatorInset;
    @end
    
    @implementation UITableView (WJAdditions)
    -(void) wjZeroSeparatorInset {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
      if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
        self.layoutMargins = UIEdgeInsetsZero;
      }
    #endif
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
       if ([self respondsToSelector: @selector(setSeparatorInset:)])
          self.separatorInset = UIEdgeInsetsZero;
       }
    #endif
    }
    @end
    
  • 在UITableView中,初始化后不久,调用

    [self wjZeroSeparatorInset];
    
  • 在UITableViewController中,您需要以下内容:

    if ([UITableViewCell instancesRespondToSelector:@selector(setLayoutMargins:)]) {
        [[UITableViewCell appearance]setLayoutMargins:UIEdgeInsetsZero];
    }
    
    -(void) viewWillLayoutSubviews {
      [super viewWillLayoutSubviews];
      UITableView* tv = self.tableView;
      if ([tv respondsToSelector:@selector(setLayoutMargins:)]) {
        [tv setLayoutMargins:UIEdgeInsetsZero];
      }
    }
    
  • 解决方案:

    -(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在单元格和表视图上引入了layoutMargins属性。

    此属性在iOS 7.0中不可用,因此您需要确保在分配它之前进行检查

    然而,苹果已经在你的单元格中添加了**属性,这将阻止它继承你的表格视图的边距设置。这样,单元格就可以独立于表格视图配置自己的页边距。将其视为一种覆盖

    此属性称为PreserveSPerViewLayoutMargins,如果将其设置为“否”,则可以使用自己单元格的layoutMargin设置替代表视图的layoutMargin设置。它既节省了时间(您不必修改表视图的设置),又更加简洁。请参考迈克·阿卜杜拉的答案以了解详细解释

    注:正如迈克·阿卜杜拉(Mike Abdullah)的回答所述,这是正确的、不太混乱的实现;设置单元格的PreserveSPerViewLayoutMargins=NO将确保表视图不会覆盖单元格设置

    第一步-设置单元格边距:

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    /*
        Tells the delegate 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 seperator 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
        }
    }
    
    /*
        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
        }
    }
    
    将单元格上的PreserveSPerViewLayoutMargins属性设置为NO应该可以防止表视图覆盖单元格边距。在某些情况下,它似乎不能正常工作

    第二步-只有当所有操作都失败时,您才可以强制执行表视图边距:

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    /*
        Tells the delegate 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 seperator 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
        }
    }
    
    /*
        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
        }
    }
    
    …就这样!这应该适用于iOS 8和iOS 7

    注意:使用iOS 8.1和7.1进行测试,在我的例子中,我只需要使用本说明的第一步

    第二步仅在渲染单元下有未填充的单元时才需要,即,如果表格大于表格模型中的行数。不执行第二步将导致不同的分隔符偏移

    @implementation UITableView (HGApperance) 
    -(void)setSeparatorXMargin:(CGFloat)margin{
        // iOS 7
        if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
            [self setSeparatorInset:UIEdgeInsetsZero];
        }
    
        if ([self respondsToSelector:@selector(layoutMargins)]) {
            self.layoutMargins = UIEdgeInsetsZero;
        }
    }
    @end
    
    对于TableviewCell,Willam的工作非常完美。

    到iOS8及以上版本

    如果您发现所有的解决方案都不如我,我认为您可能使用了
    UITableViewCell
    的子类并重写了
    layoutSubviews
    方法,如果您满足两个条件,请继续阅读

    一步一步地做或检查

    1、 在alloc和init
    UITableView
    之后,设置其
    layoutMargins
    属性

    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        _tableView.layoutMargins = UIEdgeInsetsZero ;
    }
    
    2、 在方法
    -(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
    中,在alloc和init您的自定义
    UITableViewCell
    之后,设置其
    layoutMargins
    属性

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        cell.layoutMargins = UIEdgeInsetsZero ;
    }
    
    3、 最重要的部分进来了。如果覆盖UITableViewCell的
    -(void)layoutSubviews
    方法,不要忘记调用它的super

    - (void)layoutSubviews
    {
        [super layoutSubviews] ;
    
        // layout the subviews
    }
    
    Xamarin.iOS版本 在Luis E.Prado和King Wizard的帖子帮助下,我在iOS 8.4上扩展了Xamarin.iOS中的UITableView分隔符的全宽。我必须设置SeparatorInset和LayoutMargins才能让它工作。我没有对较低的iOS版本进行检查,因为我的目标是iOS 8及以上版本

    我编写了以下两个C#扩展方法来在UITableView或UITableViewCell级别设置UITableView分隔符的全宽。您可以使用一个或两个