我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
在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);
}
其他回答
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
对于Swift 2:
将以下行添加到viewDidLoad():
tableView.separatorColor = UIColor.clearColor()
更简单、更符合逻辑的做法是:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
在大多数情况下,您不希望只看到最后一个表视图单元格分隔符。这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符嵌入。
我能找到的唯一解决办法就是下一个
extension UITableViewCell {
func separator(hide: Bool) {
separatorInset.left = hide ? self.bounds.width * 1.5 : 16 // or whatever you want to be the left inset
}
}
我不知道为什么,但是self.bounds.width没有像预期的那样工作,所以我乘以1.5。
如果你想隐藏一个单元格,你只需要这样做:
cell.separator(hide: true)
对于其余的单元格,只发送参数为false
cell.separator(hide: false)
在iOS9上,我有一个问题,改变分隔符插入也会影响文本和细节标签的定位。
我用这个解出来了
override func layoutSubviews() {
super.layoutSubviews()
separatorInset = UIEdgeInsets(top: 0, left: layoutMargins.left, bottom: 0, right: width - layoutMargins.left)
}