我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
正如(许多)其他人指出的那样,你可以通过简单地关闭整个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中。
其他回答
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);
}
}
如果你的应用程序是可旋转的-使用3000.0f作为左插入常数或在运行中计算它。 如果你尝试设置右插入,你会在iOS 8的单元格左侧看到分隔符。
如果接受的答案不成立,你可以试试这个:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f; }
太棒了;)
你必须采取自定义单元格,并添加标签和设置约束,如标签应覆盖整个单元格区域。 并在构造函数中编写下面的行。
- (void)awakeFromNib {
// Initialization code
self.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
//self.layoutMargins = UIEdgeInsetsZero;
[self setBackgroundColor:[UIColor clearColor]];
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
}
也设置UITableView布局边距如下
tblSignup.layoutMargins = UIEdgeInsetsZero;
使用这个子类,set separatorInset在iOS 9.2.1中不起作用,内容会被压缩。
@interface NSPZeroMarginCell : UITableViewCell
@property (nonatomic, assign) BOOL separatorHidden;
@end
@implementation NSPZeroMarginCell
- (void) layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
if (![view isKindOfClass:[UIControl class]]) {
if (CGRectGetHeight(view.frame) < 3) {
view.hidden = self.separatorHidden;
}
}
}
}
@end
https://gist.github.com/liruqi/9a5add4669e8d9cd3ee9
更简单、更符合逻辑的做法是:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
在大多数情况下,您不希望只看到最后一个表视图单元格分隔符。这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符嵌入。