UITableView
があり、その中に3行しかありません。これらの3行を見ることができます。問題は空のセルです。そこに線が見えます。私はそれらの線を見たくありません。
これらの行を削除する方法はありますか?
以下は私が探しているものの画像です。
以下のコードスニペットのいずれかを使用して、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まで)で動作しています。
Andrey Zの返事よりも簡単:UITableViewクラスで偽のtableFooterViewを作成するだけです:
self.tableFooterView = [UIView new]; // to hide empty cells
とスウィフト:
tableFooterView = UIView()
SwiftおよびiOS 9の回答を更新しました。これは.Plain
さまざまなテーブルビューで機能します。
tableView.tableFooterView = UIView()
透明な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];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
空のセルの区切り線を削除するには、このコードを使用します。
- (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];
}
viewForFooterInSection
で空のUIView()
を返すだけでうまくいきました:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
次のコードを試してください:
self.tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
if ([self.tblView respondsToSelector:@selector(setSeparatorInset:)])
{
[self.tblView setSeparatorInset:UIEdgeInsetsZero];
}
// write in view did load
使用できます
tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
それは私のために働いた...
以前の提案のいくつかには、大きな概念エラーが含まれています。
もしあなたがそうするなら:
[セル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)
}
ストーリーボードを使用している場合は、UIViewをセルの下のUITableViewにドラッグアンドドロップし、高さを0に設定するだけです(iOS 8プロジェクトでのみテスト済みです)。
CellForRowAtIndexPath内
let separatorLineView:UIView = UIView(frame: CGRectMake(0,0,self.tableview.bounds.width,0.5))
separatorLineView.backgroundColor = tableView.separatorColor
cell!.contentView.addSubview(separatorLineView)
提案された答えはどれも、私の同様の問題に適していませんでした。 tableViewデリゲートでこのメソッドを実装すると、最終的に機能しました:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
テーブルビューをプレーンではなくグループ化するように設定できます。これにより、外観は少し変更されますが、少なくとも余分な行は削除されます。
私はこの正確な問題を抱えていたため、グループ化されたビューに変更しました。ずっと良く見えます。
Swiftソリューション:tableView.tableFooterView = UIView(frame:CGRectZero)
Xcode 7.2で動作しました
これがあなたが探しているものだと思います。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1.0f;
}