我是iOS开发人员的新手,我想在官方设置应用中使用某些单元格之间的间距。
有没有办法在一个TableView中执行此操作,还是必须插入更多这些?
如果我有更多的TableView,屏幕不能完全滚动,也许并非所有的都能显示。
答案 0 :(得分:9)
要获得与设置中的TableView
相似的Style
,您必须将TableView
的{{1}}设置为Grouped
并使用Sections
分离/分组某组TableViewCells
。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myNumberOfRows
}
func numberOfSections(in tableView: UITableView) -> Int {
return myNumberOfSections
}
有关numberOfSections
的更多信息,请查看UITableViewDataSource代表。
答案 1 :(得分:0)
有两个选项:
如果您使用static
表格视图:创建空单元格作为间距
如果您使用的是normal (dynamic)
表格视图:
将空单元格创建为间距
Emply Space是一个标题视图,请记住设置表格样式为Group
答案 2 :(得分:0)
如果你希望节之间有间隙,你可以简单地使用UITableView的heightForFooterInSection方法。这将在每个部分之间产生差距。如果您希望拥有自定义视图(如自定义颜色或您选择的图像),您可以使用viewForFooterInSection方法并返回所需的以编程方式创建的视图或创建xib(如果您创建了一个xib,则需要registerNib以便UIViewController识别xib)。您可以使用超类是UIView的每个UI元素。
答案 3 :(得分:0)
如果您希望各部分之间使用默认分隔符,请将tableView分组。要自定义这些头,请实现UITableViewDelegate的这两个方法,并根据需要更改值。例如,设置app作为某些分节符上的标签而不是空视图:
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
}