私のアプリでは、UITableView
私の問題は、最後のセルの最後の境界線を削除したいということですUITableView
。
次の画像を確認してください。
私のアプリでは、UITableView
私の問題は、最後のセルの最後の境界線を削除したいということですUITableView
。
次の画像を確認してください。
最善の解決策は、フッター ビューを追加することです。次のコードは、最後のセルの行を完全に非表示にします。
Objective-C
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
スイフト4.0
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
iOS 7 では、より簡単な解決策があります。cell が最後のセルであるとします。
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
2015 年 9 月 14 日更新。私の元の答えは時代遅れになりますが、それはまだすべての iOS バージョンの普遍的なソリューションです。
の標準の区切り線を非表示tableView
にして、各セルの上部にカスタムの線を追加できます。カスタム セパレーターを追加する最も簡単な方法は、UIView
高さ 1px のシンプルなものを追加することです。
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:separatorLineView];
今日まで、セルの下に余分なセパレーターを隠すための別の方法を購読しています(iOS 6.1以降で機能します):
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
これは、iOS 7および8で機能します。
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));
注:tableView.bounds
呼び出すタイミングによっては間違った値を報告することがあるので、使用には注意してください。ランドスケープ モードでの不正な境界の報告を参照してください。
私の短いバージョン:
self.tblView.tableFooterView = [UIView new];
iOS 7 以降の場合
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL bIsLastRow = NO;
//Add logic to check last row for your data source
NSDictionary *dict = [_arrDataSource objectAtIndex:indexPath.section];
if([_arrDataSource lastObject] == dict)
{
bIsLastRow = YES;
}
//Set last row separate inset left value large, so it will go outside of view
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
{
[cell setSeparatorInset:UIEdgeInsetsMake(0, bIsLastRow ? 1000 :0, 0, 0)];
}
}
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
別のオプションは、サブクラス化によるものUITableView
です。
@synthesize hideLastSeparator = _hideLastSeparator;
@synthesize hideLastSeparatorView = _hideLastSeparatorView;
-(void)setHideLastSeparator:(BOOL)hideLastSeparator {
if (_hideLastSeparator == hideLastSeparator) {
return;
}
_hideLastSeparator = hideLastSeparator;
if (_hideLastSeparator) {
_hideLastSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentSize.height, self.bounds.size.width, 0.5f)];
_hideLastSeparatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
_hideLastSeparatorView.backgroundColor = self.backgroundColor;
[self addSubview:_hideLastSeparatorView];
[self hideSeparator];
}
else {
[_hideLastSeparatorView removeFromSuperview];
_hideLastSeparatorView = nil;
}
}
-(void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
if (_hideLastSeparator) {
[self hideSeparator];
}
}
-(void)hideSeparator {
CGRect frame = _hideLastSeparatorView.frame;
frame.origin.y = self.contentSize.height - frame.size.height;
_hideLastSeparatorView.frame = frame;
}
hideLastSeparator
.h には、およびのプロパティ宣言のみを含める必要がありhideLastSeparatorView
ます。
セパレーターを非表示にする場合は、新しいクラスと set を使用しますmyTableView.hideLastSeparator = YES
。
これが機能する方法は、新しいビューを追加して最後のセパレーターを妨害することです。
これは、私の意見では (カスタムセパレーターを使用したり、最後のセルの最後のセパレーターインセットを設定したりするよりも) やや使いやすく、設定方法によってtableFooterView
時々発生する奇妙なアニメーションを回避します (たとえば、行の挿入/削除中または他のテーブル中) 。アニメーション)。
デリゲートの最後のセル インデックスを見つけて、cellForRow
以下のコード行を挿入します。
cell.separatorInset = UIEdgeInsetsMake(
0.f,
40.0f,
0.f,
self.view.frame.size.width-40.0f
);
これは私にとってうまくいきます:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? yourCell else {
return
}
// Here we're checking if your cell is the last one
if indexPath.row == numberOfCells.count - 1 {
cell.separatorInset.left = UIScreen.main.bounds.width
}
}
ここで行っているのは、左インセットのサイズを十分に大きな値に設定して、区切り線が表示されないようにすることです。したがって、インセットのサイズ値を大きくすると、左インセットが右方向に向かうため、画面の右にどんどん近づきます。
Tony Xuの回答を拡張すると、複数のセクションがあり、これは最後のセルセパレーターを削除するために機能するようです
if(indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1)
{
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
}
cellForRowAtIndexPath
データソース内