我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
来跟进Hiren的回答。
在ViewDidLoad和下面一行:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
或者,如果你正在使用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];
这是一个结果的例子,我显示一个动态单元格的tableview(但只有一个内容)。结果是,只有一个有分隔符,而不是所有的“虚拟”tableview自动添加填充屏幕。
编辑:对于那些不经常阅读注释的人来说,实际上有一种更好的方法,只需几行代码:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
其他回答
在tableview单元格类中。放这行代码
separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: self.bounds.size.width)
在viewDidLoad中添加这一行:
self.tableView.separatorColor = [UIColor clearColor];
在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];
}
适用于iOS 7以上版本(包括iOS 8)
if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
iphone的宽度是320。因此,在单元格属性中为separatorInset设置左右值,大于320的一半。
我的开发环境是
Xcode 7.0 7A220 Swift 2.0 iOS 9.0
以上答案并不完全适用于我
经过尝试,我最终的工作解决方案是:
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
结果是:
cell.separatorInset = UIEdgeInsetsMake(0.0, cell.bounds.size.width, 0.0, -cell.bounds.size.width)
在iOS 10.2中运行良好