当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当前回答
Swift适用于:
tableView.tableFooterView = UIView()
其他回答
你可能会找到这个问题的很多答案。大多数都围绕着UITableView的tableFooterView属性进行操作这是隐藏空行的合适方法。为了方便起见,我创建了一个简单的扩展,它允许打开/关闭接口生成器中的空行。 你可以从这个要点文件里查到。我希望它能为你节省一点时间。
extension UITableView {
@IBInspectable
var isEmptyRowsHidden: Bool {
get {
return tableFooterView != nil
}
set {
if newValue {
tableFooterView = UIView(frame: .zero)
} else {
tableFooterView = nil
}
}
}
}
用法:
tableView.isEmptyRowsHidden = true
在Swift中(我使用4.0),你可以通过创建一个自定义UITableViewCell类来实现这一点,并覆盖setSelected方法。然后分隔符将全部插入到0。(我的主类与表视图有一个清晰的背景)颜色。
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// eliminate extra separators below UITableView
self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
您可以删除空行分隔符,只需添加脚注的小高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.01
}
这里有另一种不使用分组表样式的方法,一种您可能猜不到的方法。向表中添加页眉和页脚(也许一个或另一个就足够了,还没有选中)会导致分隔符从填充行/空白行中消失。
我无意中发现了这一点,因为我想在桌子的顶部和底部留出一点空间,以减少触碰按钮的风险,而不是手指肥大的表格单元格。下面是一个将空白视图作为页眉和页脚插入的方法。使用您喜欢的任何高度,您仍然可以消除额外的分隔线。
- (void) addHeaderAndFooter
{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.myTableView setTableHeaderView:v];
[self.myTableView setTableFooterView:v];
[v release];
}
In response to @Casebash, I went back to the code in my app ("AcmeLists" List Manager in iTunes store...) and short-circuited the addHeaderAndFooter method to verify. Without it, I have the extra row separators; with the code, I have what you see in this window snap: no table row separators picture. So I'm not sure why it wouldn't have worked for you. Moreover, it makes sense to me that having any custom footer on a table view would necessarily have to stop drawing row separators for blank rows below it. That would be hideous. For reference, I looked at tables where there were more rows than could be viewed on screen, and then for a table with two rows. In both cases, no extraneous separators.
也许您的自定义视图实际上并没有添加。要检查这一点,将背景颜色设置为clearColor以外的颜色,例如,[UIColor redColor]。如果您在表格底部没有看到一些红色条,则表示您的页脚没有设置。
如果你要子类化UITableView,你需要这样做…
-(void)didMoveToSuperview {
[super didMoveToSuperview];
self.tableFooterView = [UIView new];
}