我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
在使用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)
}
}
Note: this snippet above will work on UITableView using dynamic cells. The only problem that you can encounter is when you use static cells with categories, a separator type different than none and a grouped style for the table view. In fact, in this particular case it will not hide the last cell of each category. For overcoming that, the solution that I found was to set the cell separator (through IB) to none and then creating and adding manually (through code) your line view to each cell. For an example, please check the snippet below:
/*
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)
}
}
其他回答
对于iOS7或更高版本,更简洁的方法是使用INFINITY而不是硬编码的值。当屏幕旋转时,您不必担心更新单元格。
if (indexPath.row == <row number>) {
cell.separatorInset = UIEdgeInsetsMake(0, INFINITY, 0, 0);
}
在iOS9上,我有一个问题,改变分隔符插入也会影响文本和细节标签的定位。
我用这个解出来了
override func layoutSubviews() {
super.layoutSubviews()
separatorInset = UIEdgeInsets(top: 0, left: layoutMargins.left, bottom: 0, right: width - layoutMargins.left)
}
正如(许多)其他人指出的那样,你可以通过简单地关闭整个UITableView本身来轻松隐藏所有UITableViewCell分隔符;例如在你的UITableViewController中
- (void)viewDidLoad {
...
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
...
}
不幸的是,在每个单元格的基础上,这是一个真正的PITA,这是你真正要求的。
就我个人而言,我已经尝试了许多改变cell.separatorInset的排列。正如(许多)其他人所建议的那样,再次向左,但问题是,引用苹果的话(强调):
"...您可以使用此属性在当前单元格内容与表的左右边缘之间添加空格。正插入值将单元格内容和单元格分隔符向内移动,远离表格边缘……”
因此,如果你试图通过将分隔符推到屏幕的右侧来“隐藏”分隔符,你最终也会缩进你的单元格的contentView。正如crifan所建议的,您可以尝试通过设置cell来补偿这种讨厌的副作用。indentationWidth和cell。indentationLevel适当地将所有内容移回,但我发现这也是不可靠的(内容仍然缩进…)
我发现的最可靠的方法是在一个简单的UITableViewCell子类中覆盖layoutSubviews,并设置右插入,使它命中左插入,使分隔符有0宽度,因此不可见[这需要在layoutSubviews中自动处理旋转]。我还向我的子类添加了一个方便的方法来打开它。
@interface MyTableViewCellSubclass()
@property BOOL separatorIsHidden;
@end
@implementation MyTableViewCellSubclass
- (void)hideSeparator
{
_separatorIsHidden = YES;
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (_separatorIsHidden) {
UIEdgeInsets inset = self.separatorInset;
inset.right = self.bounds.size.width - inset.left;
self.separatorInset = inset;
}
}
@end
警告:没有一个可靠的方法来恢复原始的右插入,所以你不能“取消隐藏”分隔符,因此我为什么要使用一个不可逆的hideSeparator方法(vs暴露separatorIsHidden)。请注意,separatorInset在重用的单元格之间持续存在,因为你不能“取消隐藏”,你需要将这些隐藏的分隔单元格隔离在它们自己的reuseIdentifier中。
长筒5 . 13+
当你定义你的表时,只需添加:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
// Removes separator lines
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
return UIView()
}
神奇的一行是tableView。separatorStyle = UITableViewCell.SeparatorStyle.none
我不相信这种方法在任何情况下都适用于动态单元格……
if (indexPath.row == self.newCarArray.count-1) {
cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
不管你在哪个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)
}
这样你就可以在每次加载时更新数据结构状态