70 iphone xcode objective-c uitableview ios
在我的应用程序中,我使用了一个UITableView
我的问题是我要删除最后一个单元格的最后一个边框UITableView.
请检查以下图片:
Ton*_* Xu 129
最好的解决方案是添加页脚视图.以下代码完美隐藏了最后一个单元格的行:
Objective-C的
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
Run Code Online (Sandbox Code Playgroud)
dav*_*sdk 41
在iOS 7中,有一个更简单的解决方案.假设细胞是你的最后一个细胞:
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
Run Code Online (Sandbox Code Playgroud)
one*_*ray 22
2015年9月14日更新.我的原始答案已经过时,但它仍然是所有iOS版本的通用解决方案:
您可以隐藏tableView标准分隔线,并在每个单元格的顶部添加自定义行.添加自定义分隔符的最简单方法是添加UIView1px高度的简单:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:separatorLineView];
Run Code Online (Sandbox Code Playgroud)
到目前为止,我订阅了另一种隐藏单元格下方的额外分隔符的方法(适用于iOS 6.1+):
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Run Code Online (Sandbox Code Playgroud)
Jam*_*est 13
这适用于iOS 7和8:
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));
Run Code Online (Sandbox Code Playgroud)
注意:请谨慎使用,tableView.bounds因为它有时会报告错误的值,具体取决于您何时调用它.请参阅横向模式中报告错误边界
在中添加此代码行viewDidLoad()。
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001))
Run Code Online (Sandbox Code Playgroud)
这确保了最后一个分隔符将被替换,并且替换(不可见)的页脚视图将不会占据任何额外的高度。由于页脚视图的宽度将由管理UITableView,因此您可以将其设置为0。
基于扩展的 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)))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45691 次 |
| 最近记录: |