【发布时间】:2012-08-23 11:26:58
【问题描述】:
在我的应用程序中,我使用 UITableView
我的问题是我想删除UITableView中最后一个单元格的最后一个边框。
请检查以下图片:
【问题讨论】:
-
更改 separatorInset 如此答案 stackoverflow.com/a/8561820/1418457
标签: iphone objective-c ios xcode uitableview
在我的应用程序中,我使用 UITableView
我的问题是我想删除UITableView中最后一个单元格的最后一个边框。
请检查以下图片:
【问题讨论】:
标签: iphone objective-c ios xcode uitableview
最好的解决方案是添加页脚视图。以下代码完美隐藏了最后一个单元格的行:
Objective-C
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
Swift 4.0
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
【讨论】:
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 数据源中
1.0f 作为-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 委托中的页脚高度传递。现在它就像一个魅力。赞成
在 iOS 7 中有一个更简单的解决方案。假设单元格是你的最后一个单元格:
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];
迄今为止,我订阅了 another way 以在单元格下方隐藏额外的分隔符(适用于 iOS 6.1+):
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
【讨论】:
这适用于 iOS 7 和 8:
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));
注意:使用tableView.bounds 时要小心,因为它有时会根据您调用它的时间报告错误的值。见Reporting incorrect bounds in landscape Mode
【讨论】:
cell!.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.tableView.bounds)-self.view.layoutMargins.left)
在viewDidLoad()中添加这行代码。
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001))
这确保最后一个分隔符将被替换,并且被替换(不可见)的页脚视图不会占用任何额外的高度。由于footer view的宽度由UITableView管理,所以可以设置为0。
【讨论】:
.zero 在表格底部留下了一个分隔符。
CGRect 扩展是一个很好的做法。如果你不喜欢示例代码中那些繁琐的0,你可以这样写:var frame = CGRect.zero \n frame.size.height = 0.001
.zero 相同的方式,但使用实际的零 (0.001) 来满足您的需要。
CGRect.myZero 并不能很好地说明它可以做什么。跨度>
almostZero,如果那是它不灵活的原因。您可以在static let ... 上方添加一些评论来描述它(使用 3 / 这样您就可以通过 xCode 的自动完成界面看到它)。
基于扩展的 Swift 4 解决方案,在 iOS 12 上测试
我注意到设置 height = 1 的空视图也会删除最后一个可见单元格的分隔符,但设置 height = 0 只会删除空单元格的分隔符
extension UITableView {
func removeSeparatorsOfEmptyCells() {
tableFooterView = UIView(frame: .zero)
}
func removeSeparatorsOfEmptyCellsAndLastCell() {
tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 1)))
}
}
【讨论】:
我的短版:
self.tblView.tableFooterView = [UIView new];
【讨论】:
在 Swift 中:
tableView.tableFooterView = UIView()
【讨论】:
UITableViewController 的子类
这是我目前使用的补救措施。这可能不是最好的方法,但它确实有效。
移除 SeparatorColor 和 setSeparatorStyle 以制作自定义分隔符
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setSeparatorColor:[UIColor clearColor]];
}
为每个带有表格视图背景的单元格添加自定义分隔符
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
...
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-1, 320, 1)];
separatorLineView.backgroundColor = self.tableView.backgroundColor;
[cell.contentView addSubview:separatorLineView];
return cell;
}
【讨论】:
适用于 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)];
}
}
【讨论】:
斯威夫特 4.2
要在最后一个单元格中获得从左到右的分隔符,请将其添加到您的 UITableViewDataSource's tableView(_:cellForRowAt:) 实现中:
if tableView.numberOfRows(inSection: indexPath.section) - 1 == indexPath.row {
cell.separatorInset = .zero
}
如果您不想为某个特定单元格设置分隔符,请考虑绘制自己的分隔符并设置tableView.separatorStyle = .none。
【讨论】:
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
【讨论】:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) {
cell.separatorInset.right = cell.bounds.size.width
}
}
【讨论】:
Xcode 12、Swift 5
要删除最后一个单元格后的分隔符,请选择分组的 UITableView 样式 You can do that in Interface Builder 或在 viewDidLoad 中设置
tableView.style = UITableView.Style.grouped
删除第一个单元格之前的空白,在 viewDidLoad 中编写此代码:
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 0.1))
要删除单元格之前的空白空间,并且您要在导航栏下滚动它或在避免空白空间方面遇到其他困难,请尝试将您的表格限制为控制器的超级视图 Here double click the top constraint 然后选择 'SuperView.Top' at the constraint settings
如果您想要全宽分隔符或您自己的宽度,只需选择Separator Inset作为自定义,并配置为您的值,或将其写入viewDidLoad:
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
【讨论】:
在cellForRow 委托中找到最后一个单元格索引,并将代码行放在下面:
cell.separatorInset = UIEdgeInsetsMake(
0.f,
40.0f,
0.f,
self.view.frame.size.width-40.0f
);
【讨论】:
另一种选择是继承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;
}
.h 应该只包含hideLastSeparator 和hideLastSeparatorView 的属性声明。
当想要隐藏分隔符时,使用新类并设置myTableView.hideLastSeparator = YES。
它的工作方式是通过在最后一个分隔符上添加一个新视图来阻止它。
在我看来这更容易使用(比使用自定义分隔符,或设置最后一个单元格的最后一个 separatorInset),并且避免了设置 tableFooterView 的方法有时会导致的一些奇怪的动画(例如在行期间插入/删除或其他表格动画)。
【讨论】:
如果您为 UITableViewCell 使用自定义分隔符,最好的解决方案是:
separatorView
class YourCell: UITableViewCell {
// Your implementation goes here...
// Separator view in cell
@IBOutlet private weak var separatorView: UIView!
// This function hides the separator view
func hideSeparator() {
separatorView.isHidden = true
}
}
tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 中计算单元格是否是最后一个单元格并隐藏它的分隔符public 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 == tableView.numberOfRows(inSection: indexPath.section) - 1 {
// if true -> then hide it
cell.hideSeparator()
}
}
【讨论】:
这对我很有用:
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
}
}
我们在这里所做的是将左插图的大小设置为足够大的值,以便分隔线不再可见。因此,当我们增加 inset 的 size 值时,它会越来越靠近屏幕右侧,因为左 inset 朝着正确的方向。
【讨论】:
最合适和最简单的方法是在cellForRowAt使用它
cell.drawBottonLine = (indexPath.row != sections[0].rows.count - 1)
【讨论】:
扩展 Tonny 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 数据源中
【讨论】:
tableFooterView 存在于整个表格中,而不仅仅是最后一个单元格。总的来说,hacky 解决方案,可能并不总是有效。