我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
长筒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
其他回答
当我使用扩展和调用layoutSubviews()立即更新布局视图时,它为我工作。
extension UITableViewCell {
func removeSeparator() {
separatorInset = UIEdgeInsetsMake(0, bounds.size.width, 0, 0)
}
}
override func layoutSubviews() {
super.layoutSubviews()
removeSeparator()
}
在UITableViewDataSource的cellForRowAtIndexPath方法中
迅速:
if indexPath.row == {your row number} {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
or :
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)
对于默认保证金:
cell.separatorInset = UIEdgeInsetsMake(0, tCell.layoutMargins.left, 0, 0)
显示端到端的分隔符
cell.separatorInset = .zero
objective - c:
if (indexPath.row == {your row number}) {
cell.separatorInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, CGFLOAT_MAX);
}
更简单、更符合逻辑的做法是:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
在大多数情况下,您不希望只看到最后一个表视图单元格分隔符。这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符嵌入。
试试下面的代码,也许能帮你解决问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* reuseIdentifier = @"Contact Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (indexPath.row != 10) {//Specify the cell number
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithLine.png"]];
} else {
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithOutLine.png"]];
}
}
return cell;
}
我不相信这种方法在任何情况下都适用于动态单元格……
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)
}
这样你就可以在每次加载时更新数据结构状态