web-dev-qa-db-ja.com

Xcode 6 iPhone SimulatorのiOS 8 UITableViewでSeparatorInsetを削除します

Xcode 6 GMでiPhone 6 Simulator(iOS 8)のUITableViewに奇妙な空白が見つかりました。ストーリーボードとコードの両方から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];
    }
}

以下にスクリーンショットを添付しました:

iPhone 6 Simulator weird white space on tableview

ところでAutoLayoutを使用しています。誰かがTableViewの奇妙な空白を削除する方法を教えてくれることを願っています。

61
Ricky

このself.myTableView.layoutMargins = UIEdgeInsetsZero;を試して、正しい方向を教えてくれた学生に感謝します。」 layoutMarginsはiOS 8からのみ使用できるため、このコード行はiOS 8でのみ動作します。iOS7で同じコードを実行するとクラッシュします。

@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

以下は、tableview layoutMarginsおよびcell layoutMarginsが存在する場合、UIEdgeInsetsZeroとして設定することにより、この奇妙な空白を解決する正しい答えです(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];
   }
}

以下のスクリーンショットをご覧ください:-

enter image description here

117
Ricky

UITableViewCellクラスカテゴリを作成して、このゲッターを追加してみてください

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

iOS7では、これはcosと呼ばれません。SDKにはこのプロパティはありません。クラッシュは発生しません。iOS8では、セルを使用するたびに呼び出されます

わたしにはできる

49
Will Zhang

わずか3行のコードでの私のソリューション:

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

IOS8では、コンテンツマージンの設定という名前の新しい概念が導入され、---という名前の新しいプロパティが導入されました layoutMarginsも導入されています。プロパティの詳細については、Apple Docを参照してください。layoutMarginsUIEdgeInsetsであり、デフォルトでは値は{8,8,8,8}です。 IOS8でTableViewのセパレータ行を削除するには、tableView.seperatorInset = UIEdgeInsetsZeroを設定することに加えて、次のようにする必要もあります。

最初にマクロを定義する

#define isIOS8SystemVersion (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)

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

    }

これらを実行すると、セパレーター行が削除されます。次のようにすることもできます:

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

そして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;
     }
}
16
monjer

Xcode 6.2の私の場合、Will Qの答えに加えて、Main.storyboardに移動し、UITableViewCell> Attributes Inspectorを選択する必要があります。 [区切り]ドロップダウンリストを[デフォルトのインセット]からカスタムインセットに変更します。左のインセットを15から0に変更します。

enter image description here

12
voratima

IOS 7およびiOS 8の回避策

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

IOS8では、行レベルとテーブルレベルでInset bothを設定する必要があります。

CellForRowAtIndexPathの行レベル:

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

ViewDidLoadのテーブルレベル:

[tableReference setSeparatorInset:UIEdgeInsetsZero];

その後、プロジェクトをクリーンアップすることをお勧めします。場合によっては、これらの変更がシミュレータの実行可能アプリに直接導入されていないことに気付きました。

9
Vincent

iOS 8の

cellForRowAtIndexPathメソッドにcell.layoutMargins = UIEdgeInsetsZero;を設定して試してください

5
Yogendra

白い線を削除したいが、セパレーターのインセットをそのままにしたい場合は、cell.backgroundColorをtableView backgroundColorに設定するだけです。 cell.contentView.backgroundColorを設定するだけでは、問題は消えません。

3
honcheng

私は多くの方法を試しましたが、どれも機能しませんが、これは私のために機能します。

 WORK FOR ME

1
Harshad

以下を追加してください:

tableView.separatorStyle = .none
0
J. Doe

IPhone 6およびPlusのスタートアップイメージを追加します。画像が追加される前は、電話はスケーリングモードで実行されています。つまり、古いアプリは新しい画面サイズに合わせてスケーリングされます。これが線の原因である可能性があります。新しい画像は、Retina HD 5.5(iPhone6Plus)1242x2208およびRetina HD 4.7(iPhone6)750x1334です。

0

静的UITableViewがあり、セルセパレーターのマージンを左端に移動したかった。

上記の回答のおかげで、これは私の解決策でした

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // needed to shift the margin a specific cell to the left Edge
    if indexPath.section == 3 && indexPath.row == 0 {
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.separatorInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
    }
}
0
DogCoffee

IOS7とiOS8の両方でUITableViewセパレーターのインセットをゼロにするには、コードを変更する代わりに、View-> Mode-> Aspect Fillを変更してUITableViewのxibを変更します。

0
strohtennis

参照 iOS 8 UITableView separator inset 0 not working

基本的に、cell.layoutMarginとtableViewのlayoutMarginの両方を設定する必要があります。 YMMVですが、動作する前にlayoutSubviewsでテーブルビューを設定する必要がありました。

0
cdstamper