我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
对于Swift 2:
将以下行添加到viewDidLoad():
tableView.separatorColor = UIColor.clearColor()
其他回答
在你的UITableViewCell子类中,覆盖layoutSubviews并隐藏_UITableViewCellSeparatorView。适用于iOS 10。
override func layoutSubviews() {
super.layoutSubviews()
subviews.forEach { (view) in
if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
view.hidden = true
}
}
}
当我使用扩展和调用layoutSubviews()立即更新布局视图时,它为我工作。
extension UITableViewCell {
func removeSeparator() {
separatorInset = UIEdgeInsetsMake(0, bounds.size.width, 0, 0)
}
}
override func layoutSubviews() {
super.layoutSubviews()
removeSeparator()
}
迅速:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
// remove separator for last cell
cell.separatorInset = indexPath.row < numberOfRowsInSection-1
? tableView.separatorInset
: UIEdgeInsets(top: 0, left: tableView.bounds.size.width, bottom: 0, right: 0)
return cell
}
objective - c:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
// remove separator for last cell
cell.separatorInset = (indexPath.row < numberOfRowsInSection-1)
? tableView.separatorInset
: UIEdgeInsetsMake(0.f, tableView.bounds.size.width, 0.f, 0.f);
return cell;
}
在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);
}
使用这个子类,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