Saf*_*ari 235 cocoa-touch objective-c uitableview separator ios
我正在定制一个UITableView
.我想隐藏最后一个单元格上的分隔线...我可以这样做吗?
我知道我可以做tableView.separatorStyle = UITableViewCellStyle.None
但这会影响tableView的所有单元格.我希望它只影响我的最后一个细胞.
Hir*_*ren 355
在viewDidLoad
,添加此行:
self.tableView.separatorColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
并在cellForRowAtIndexPath
:
适用于iOS较低版本
if(indexPath.row != self.newCarArray.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor redColor];
[cell addSubview:line];
}
Run Code Online (Sandbox Code Playgroud)
适用于iOS 7上层版本(包括iOS 8)
if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
Run Code Online (Sandbox Code Playgroud)
Avi*_*ash 234
您可以使用以下代码:
斯威夫特:
if indexPath.row == {your row number} {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
Run Code Online (Sandbox Code Playgroud)
要么 :
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)
Run Code Online (Sandbox Code Playgroud)
默认保证金:
cell.separatorInset = UIEdgeInsetsMake(0, tCell.layoutMargins.left, 0, 0)
Run Code Online (Sandbox Code Playgroud)
以端到端的方式显示分隔符
cell.separatorInset = .zero
Run Code Online (Sandbox Code Playgroud)
Objective-C的:
if (indexPath.row == {your row number}) {
cell.separatorInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, CGFLOAT_MAX);
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*min 93
跟进Hiren的回答.
在ViewDidLoad和以下行:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是XIB或Storyboard,请将" separator " 更改为" none ":
在CellForRowAtIndexPath中添加:
CGFloat separatorInset; // Separator x position
CGFloat separatorHeight;
CGFloat separatorWidth;
CGFloat separatorY;
UIImageView *separator;
UIColor *separatorBGColor;
separatorY = cell.frame.size.height;
separatorHeight = (1.0 / [UIScreen mainScreen].scale); // This assures you to have a 1px line height whatever the screen resolution
separatorWidth = cell.frame.size.width;
separatorInset = 15.0f;
separatorBGColor = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];
separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
separator.backgroundColor = separatorBGColor;
[cell addSubView: separator];
Run Code Online (Sandbox Code Playgroud)
这是一个结果示例,其中我显示了一个带有动态单元格的表视图(但只有一个带有内容的表格视图).结果是只有那个有一个分隔符而不是所有"虚拟"的tableview自动添加以填充屏幕.
希望这可以帮助.
编辑:对于那些并不总是阅读评论的人来说,实际上有一种更好的方法可以用几行代码来完成:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
Run Code Online (Sandbox Code Playgroud)
小智 51
如果您不想自己绘制分隔符,请使用以下命令:
// Hide the cell separator by moving it to the far right
cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
Run Code Online (Sandbox Code Playgroud)
此API仅从iOS 7开始提供.
cri*_*fan 29
我的发展环境是
以上答案对我不起作用
经过尝试,我最终的解决方案是:
let indent_large_enought_to_hidden:CGFloat = 10000
cell.separatorInset = UIEdgeInsetsMake(0, indent_large_enought_to_hidden, 0, 0) // indent large engough for separator(including cell' content) to hidden separator
cell.indentationWidth = indent_large_enought_to_hidden * -1 // adjust the cell's content to show normally
cell.indentationLevel = 1 // must add this, otherwise default is 0, now actual indentation = indentationWidth * indentationLevel = 10000 * 1 = -10000
Run Code Online (Sandbox Code Playgroud)
Okh*_*bay 11
在Swift 3和Swift 4中,您可以像这样编写UITableViewCell的扩展:
extension UITableViewCell {
func hideSeparator() {
self.separatorInset = UIEdgeInsets(top: 0, left: self.bounds.size.width, bottom: 0, right: 0)
}
func showSeparator() {
self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以使用如下(当单元格是您的单元格实例时):
cell.hideSeparator()
cell.showSeparator()
Run Code Online (Sandbox Code Playgroud)
最好将表格视图单元格的宽度指定为左侧插入,而不是为其分配一些随机数.因为在某些屏幕尺寸中,可能不是现在,但将来您的分隔符仍然可见,因为随机数可能不够.此外,在横向模式的iPad中,您无法保证分隔符始终不可见.
更好的iOS 7和8解决方案
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
DLog(@"");
if (cell && indexPath.row == 0 && indexPath.section == 0) {
DLog(@"cell.bounds.size.width %f", cell.bounds.size.width);
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.0f);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的应用程序是可旋转的 - 使用3000.0f作为左插入常量或动态计算.如果您尝试设置右侧插入,则在iOS 8的单元格左侧有可见的分隔符部分.
在iOS 7中,UITableView分组样式单元格分隔符看起来有点不同.看起来有点像这样:
我尝试过Kemenaran的回答:
cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
Run Code Online (Sandbox Code Playgroud)
然而,这似乎对我没有用.我不知道为什么.所以我决定使用Hiren的答案,但使用UIView
而不是UIImageView
,并以iOS 7风格绘制线条:
UIColor iOS7LineColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
//First cell in a section
if (indexPath.row == 0) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
line.backgroundColor = iOS7LineColor;
[cell addSubview:line];
[cell bringSubviewToFront:line];
} else if (indexPath.row == [self.tableViewCellSubtitles count] - 1) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
line.backgroundColor = iOS7LineColor;
[cell addSubview:line];
[cell bringSubviewToFront:line];
UIView *lineBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.view.frame.size.width, 1)];
lineBottom.backgroundColor = iOS7LineColor;
[cell addSubview:lineBottom];
[cell bringSubviewToFront:lineBottom];
} else {
//Last cell in the table view
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, self.view.frame.size.width, 1)];
line.backgroundColor = iOS7LineColor;
[cell addSubview:line];
[cell bringSubviewToFront:line];
}
Run Code Online (Sandbox Code Playgroud)
如果使用此方法,请确保在第二个if语句中插入正确的表视图高度.我希望这对某人有用.
在UITableViewCell子类中,覆盖layoutSubviews并隐藏_UITableViewCellSeparatorView.适用于iOS 10.
override func layoutSubviews() {
super.layoutSubviews()
subviews.forEach { (view) in
if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
view.hidden = true
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
在willdisplaycell
:
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
Run Code Online (Sandbox Code Playgroud)
在使用iOS 8.4 的Swift 中:
/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
if indexPath.row == 3 {
// Hiding separator line for only one specific UITableViewCell
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
}
}
Run Code Online (Sandbox Code Playgroud)
注意:上面的这个片段将使用动态单元格在 UITableView 上工作。您可能会遇到的唯一问题是,当您使用带有类别的静态单元格、分隔符类型不同于无以及表格视图的分组样式时。事实上,在这种特殊情况下,它不会隐藏每个类别的最后一个单元格。为了克服这个问题,我找到的解决方案是将单元格分隔符(通过 IB)设置为无,然后手动(通过代码)创建和添加您的线视图到每个单元格。例如,请检查以下代码段:
/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
// Row 2 at Section 2
if indexPath.row == 1 && indexPath.section == 1 {
// Hiding separator line for one specific UITableViewCell
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
// Here we add a line at the bottom of the cell (e.g. here at the second row of the second section).
let additionalSeparatorThickness = CGFloat(1)
let additionalSeparator = UIView(frame: CGRectMake(0,
cell.frame.size.height - additionalSeparatorThickness,
cell.frame.size.width,
additionalSeparatorThickness))
additionalSeparator.backgroundColor = UIColor.redColor()
cell.addSubview(additionalSeparator)
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这种方法在动态细胞的任何情况下都不会起作用。
if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
Run Code Online (Sandbox Code Playgroud)
对于动态单元格,使用哪种tableview方法都没关系,更改inset属性的单元格每次出列时始终会立即设置inset属性,从而导致大量的行分隔符丢失……直到您自己改变。
这样的事情对我有用:
if indexPath.row == franchises.count - 1 {
cell.separatorInset = UIEdgeInsetsMake(0, cell.contentView.bounds.width, 0, 0)
} else {
cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.contentView.bounds.width, 0)
}
Run Code Online (Sandbox Code Playgroud)
这样,您在每次加载时都会更新您的数据结构状态
小智 5
更简单和合乎逻辑的是这样做:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,您不想只看到最后一个表格视图单元格分隔符。而且这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符插入。
归档时间: |
|
查看次数: |
219013 次 |
最近记录: |