web-dev-qa-db-ja.com

UITableViewCellsが空の場合、区切り線を削除します

UITableViewがあり、その中に3行しかありません。これらの3行を見ることができます。問題は空のセルです。そこに線が見えます。私はそれらの線を見たくありません。

これらの行を削除する方法はありますか?

以下は私が探しているものの画像です。

Image Link

84
Fahim Parkar

以下のコードスニペットのいずれかを使用して、UITableViewの標準の区切り線を非表示にできます。カスタムセパレーターを追加する最も簡単な方法は、高さ1ピクセルの単純なUIViewを追加することです。

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // set color as you want.
[cell.contentView addSubview:separatorLineView];

または

    self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
    self.tblView.delegate=self;
    self.tblView.dataSource=self;
    [self.view addSubview:self.tblView];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.tblView setTableHeaderView:v];
    [self.tblView setTableFooterView:v];
    [v release];

または

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // This will create a "invisible" footer
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [[UIView new] autorelease];
}

または

また、 nickfalk's answer も確認してください。これも非常に短くて便利です。また、この1行も試してください。

self.tableView.tableFooterView = [[UIView alloc] init];

確かではありませんが、チェックしたiOSのすべてのバージョン(iOS 5以降、iOS 7まで)で動作しています。

88
iPatel

Andrey Zの返事よりも簡単:UITableViewクラスで偽のtableFooterViewを作成するだけです:

self.tableFooterView = [UIView new]; // to hide empty cells

とスウィフト:

tableFooterView = UIView()

146

SwiftおよびiOS 9の回答を更新しました。これは.Plainさまざまなテーブルビューで機能します。

 tableView.tableFooterView = UIView()
8
seo

透明なUIViewが1pxの高さのtableViewフッターとして機能します。

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];
7
Andrey Zverev
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
6
Robert Varga

空のセルの区切り線を削除するには、このコードを使用します。

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
     return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}   
3
Pradhyuman sinh

viewForFooterInSectionで空のUIView()を返すだけでうまくいきました:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
2
codelearner

次のコードを試してください:

self.tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
if ([self.tblView respondsToSelector:@selector(setSeparatorInset:)])
{
    [self.tblView setSeparatorInset:UIEdgeInsetsZero];
}
// write in view did load
2
Aatish Javiya

使用できます

tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

それは私のために働いた...

1
Mak083

以前の提案のいくつかには、大きな概念エラーが含まれています。

もしあなたがそうするなら:

[セルaddSubview:....

セルが「再利用」される場合でも、仕切りに新しいサブビューを追加します!

次の2つの方法で回避してください。

a)TAGを使用します。1)そのタグのサブビューを要求するDivider = cell.viewWithTag(TAG)... 2)存在する場合、別のサブビューを追加しない3)存在しない場合、追加し、タグ付けします。

b)カスタムビューを作成し、カスタムセルの「init」「awakeFromNib」にカスタムディバイダーを追加します。

a)のコード:

 if let divider = cell.viewWithTag(DIVIDER_TAG) as? UIView{
            // nothing.. eventually change color bases in IndexPath...
        }else{
            let frame = CGRectMake(0, cell.frame.height-1, cell.frame.width, 1)
            divider.tag = DIVIDER_TAG
            divider.backgroundColor = UIColor.redColor()
            cell.addSubview(divider)
        }
0
ingconti

ストーリーボードを使用している場合は、UIViewをセルの下のUITableViewにドラッグアンドドロップし、高さを0に設定するだけです(iOS 8プロジェクトでのみテスト済みです)。

0
Matoz

CellForRowAtIndexPath内

let separatorLineView:UIView = UIView(frame: CGRectMake(0,0,self.tableview.bounds.width,0.5))
separatorLineView.backgroundColor = tableView.separatorColor
cell!.contentView.addSubview(separatorLineView)
0

提案された答えはどれも、私の同様の問題に適していませんでした。 tableViewデリゲートでこのメソッドを実装すると、最終的に機能しました:

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
0
Rok Jarc

テーブルビューをプレーンではなくグループ化するように設定できます。これにより、外観は少し変更されますが、少なくとも余分な行は削除されます。

私はこの正確な問題を抱えていたため、グループ化されたビューに変更しました。ずっと良く見えます。

0
nickdnk

Swiftソリューション:tableView.tableFooterView = UIView(frame:CGRectZero)

Xcode 7.2で動作しました

0

これがあなたが探しているものだと思います。

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.0f;
}
0
Deepak Badiger