删除 iOS 8 UITableView 上的 SeparatorInset for Xcode 6 iPhone Simulator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25762723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:19:58  来源:igfitidea点击:

Remove SeparatorInset on iOS 8 UITableView for Xcode 6 iPhone Simulator

iosiphoneuitableviewios8xcode6

提问by Ricky

I found a weird white space on UITableViewfor iPhone 6 Simulator(iOS 8) on Xcode 6 GM. I have tried to set the SeparatorInsetfrom both storyboard and also the code, but the white space is till there.

我在 Xcode 6 GM上UITableViewiPhone 6 Simulator(iOS 8) 上发现了一个奇怪的空白区域。我试图SeparatorInset从故事板和代码中设置 ,但空白一直到那里。

The following code works on iOS 7 but not on iOS 8 (iPhone 6 simulator).

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

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

I attached screenshot below:

我附上了下面的截图:

iPhone 6 Simulator weird white space on tableview

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

I am using AutoLayout by the way. I hope someone can show me a way to remove the weird white space on the TableView.

顺便说一下,我正在使用 AutoLayout。我希望有人能告诉我一种方法来删除TableView.

回答by Ricky

Thanks Student for pointing me to the right direction with the comment "Try this self.myTableView.layoutMargins = UIEdgeInsetsZero;" This line of code will only work on iOS 8 because layoutMarginsis only available from iOS 8. If I run the same code on iOS 7, it will crash.

感谢学生用评论“试试这个 self.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
@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

enter image description here

在此处输入图片说明

Below is the right answer to solve this weird white space by setting the tableview layoutMarginsand cell layoutMarginsas UIEdgeInsetsZeroif it exists (for iOS 8). And it will not crash on iOS 7 as well.

下面是通过设置tableview layoutMarginscell 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];
   }
}

See the screen shot below:-

请参阅下面的屏幕截图:-

enter image description here

在此处输入图片说明

回答by Will Zhang

Try to create a UITableViewCell class category and add this getter

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

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

in iOS7 this will not be called cos there's no this property in SDK,and will not cause any crash; in iOS8 this will be called every time you use the cell

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

It works for me

这个对我有用

回答by Guido Lodetti

My solution with just three lines of code:

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

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

回答by monjer

IOS8 introduce a new concept named Configuring Content Margins, a new property named layoutMarginsis also introduced , for the details of the property , please refer to the Apple Doc . The type of layoutMarginsis UIEdgeInsets, by default the value is {8,8,8,8} . To remove the seperator line of TableView in IOS8 , in addition to set tableView.seperatorInset = UIEdgeInsetsZero, you must also do as :

IOS8引入了一个新的概念Configuring Content Margins,还引入了一个名为layoutMargins的新属性,该属性的详细信息请参考Apple Doc。layoutMargins的类型为UIEdgeInsets,默认值为 {8,8,8,8} 。在IOS8中去掉TableView的分隔线,除了set之外tableView.seperatorInset = UIEdgeInsetsZero,还必须这样做:

First define the macro

首先定义宏

#define isIOS8SystemVersion (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)

In the UITableViewDelegate method add :

在 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 ;
         }

    }

Doing these will remove the seperator line . You can also do as follow :

这样做将删除分隔线。您还可以执行以下操作:

    UITableView *tableView = [[UITableView alloc] init];
    if(isIOS8SystemVersion){
         tableView.layoutMargins = UIEdgeInsetsZero ;
    }

and in the UITableViewDelegate method add :

并在 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;
     }
}

回答by voratima

In my case in Xcode 6.2, in addition to Will Q's answer, I have to go to Main.storyboard > select the UITableViewCell > Attributes Inspector. Change Separator dropdown list from Default Insets to Custom Insets. Change the left inset from 15 to 0.

在我的 Xcode 6.2 中,除了 Will Q 的回答之外,我还必须转到 Main.storyboard > 选择 UITableViewCell > Attributes Inspector。将 Separator 下拉列表从 Default Insets 更改为Custom Insets。将左侧插图从 15 更改为 0。

enter image description here

在此处输入图片说明

回答by user3894518

Workaround for iOS 7 & iOS 8

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

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

回答by Vincent

In iOS8 you have to set the Inset bothon Row and on Table level.

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

Row level in the cellForRowAtIndexPath:

cellForRowAtIndexPath 中的行级别:

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

Table level in the viewDidLoad:

viewDidLoad 中的表级别:

[tableReference setSeparatorInset:UIEdgeInsetsZero];

After that it is a good idea to CLEAN your project. On some occasions I noted that these changes were not directly introduced in the executable App in the simulator.

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

回答by Yogendra

for iOS 8

适用于 iOS 8

try by setting cell.layoutMargins = UIEdgeInsetsZero;in cellForRowAtIndexPathmethod

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

回答by honcheng

If you want to remove the white line, but keep separator inset as it is, just set the cell.backgroundColor to the tableView backgroundColor. Just setting the cell.contentView.backgroundColor does not make the problem disappear.

如果您想删除白线,但保持分隔符插入原样,只需将 cell.backgroundColor 设置为 tableView backgroundColor。只是设置 cell.contentView.backgroundColor 不会使问题消失。

回答by Harshad

I've tried many ways, none of them work but this one works for me.

我试过很多方法,都没有用,但这个对我有用。

 WORK FOR ME

 为我工作