删除iOS 8 UITableView for XCode 6 iPhone模拟器上的SeparatorInset

Ric*_*cky 61 iphone uitableview ios xcode6 ios8

我在XCode 6 GM 上UITableViewiPhone 6模拟器(iOS 8)找到了一个奇怪的白色空间.我试图设置SeparatorInset故事板和代码,但白色空间直到那里.

以下代码适用于iOS 7,但不适用于iOS 8(iPhone 6模拟器).

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

我附上下面的屏幕截图:

桌面视图上的iPhone 6模拟器奇怪的白色空间

我顺便使用AutoLayout.我希望有人可以告诉我一种方法来消除奇怪的白色空间TableView.

Ric*_*cky 117

感谢学生通过评论" 试试这个自我.myTableView.layoutMargins = UIEdgeInsetsZero " 向我指出正确的方向.这行代码仅适用于iOS 8,因为layoutMargins仅适用于iOS 8.如果我在iOS 7上运行相同的代码,它将崩溃.

@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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

以下是通过设置来解决这个奇怪的空白正确的答案tableview layoutMargins,并cell layoutMarginsUIEdgeInsetsZero(如果它存在于iOS 8).它也不会在iOS 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];
   }
}
Run Code Online (Sandbox Code Playgroud)

见下面的屏幕截图: -

在此输入图像描述

  • 感谢您使其与iOS7兼容.适用于iOS8 (5认同)

Wil*_*ang 49

尝试创建一个UITableViewCell类类别并添加此getter

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}
Run Code Online (Sandbox Code Playgroud)

在iOS7中,这不会被称为cos,SDK中没有此属性,并且不会导致任何崩溃;在iOS8中,每次使用单元格时都会调用此属性

这个对我有用

  • 这个真的需要更多的赞成.在我的情况下工作完美. (2认同)

Gui*_*tti 23

我的解决方案只有三行代码:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)row{
    //
    // ... your code ...
    //
    if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
        cell.layoutMargins = UIEdgeInsetsZero;
        cell.preservesSuperviewLayoutMargins = false;
    }
    return cell;
}
Run Code Online (Sandbox Code Playgroud)


mon*_*jer 16

IOS8引入了一个名为配置内容边距的新概念,还引入了一个名为layoutMargins的新属性,有关该属性的详细信息,请参阅Apple Doc.layoutMargins的类型是UIEdgeInsets,默认值为{8,8,8,8}.要在IOS8中删除TableView的分隔符行,除了set之外tableView.seperatorInset = UIEdgeInsetsZero,还必须执行以下操作:

首先定义宏

#define isIOS8SystemVersion (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
Run Code Online (Sandbox Code Playgroud)

在UITableViewDelegate方法中添加:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *reuseId = @"cellReuseID" ;
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
         if(!cell){
             cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId];     
         if(isIOS8SystemVersion){   
             cell.layoutMargins = UIEdgeInsetsZero;
             cell.preservesSuperviewLayoutMargins =NO ;
         }

    }
Run Code Online (Sandbox Code Playgroud)

执行这些操作将删除分隔线.你也可以这样做:

    UITableView *tableView = [[UITableView alloc] init];
    if(isIOS8SystemVersion){
         tableView.layoutMargins = UIEdgeInsetsZero ;
    }
Run Code Online (Sandbox Code Playgroud)

并在UITableViewDelegate方法中添加:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *reuseId = @"cellReuseID" ;
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
     if(!cell){
         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseId];     
     if(isIOS8SystemVersion){   
         cell.layoutMargins = UIEdgeInsetsZero;
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,cell.preservesSuperviewLayoutMargins = NO; 是关键. (2认同)

小智 12

在我的Xcode 6.2中,除了Will Q的答案之外,我还要去Main.storyboard>选择UITableViewCell> Attributes Inspector.将分隔符下拉列表从默认插入更改为自定义插入.将左侧插入从15更改为0.

在此输入图像描述


小智 10

适用于iOS 7和iOS 8的解决方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    cell.separatorInset = UIEdgeInsetsMake(0.0f, cell.frame.size.width, 0.0f, 0.0f); 
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是正确的答案.简短又甜蜜!:) 100%工作 (2认同)

Vin*_*ent 9

在iOS8上你必须设置的插图行和表的水平.

cellForRowAtIndexPath中的行级别:

if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]){
    cell.layoutMargins = UIEdgeInsetsZero;
    cell.preservesSuperviewLayoutMargins = false;
}
Run Code Online (Sandbox Code Playgroud)

viewDidLoad中的表级别:

[tableReference setSeparatorInset:UIEdgeInsetsZero];
Run Code Online (Sandbox Code Playgroud)

之后,清理您的项目是个好主意.在某些情况下,我注意到这些更改并未直接在模拟器中的可执行应用程序中引入.


Yog*_*dra 5

对于iOS 8

通过设置尝试 cell.layoutMargins = UIEdgeInsetsZero;cellForRowAtIndexPath方法

  • 试试这个self.myTableView.layoutMargins = UIEdgeInsetsZero; (3认同)