有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
当前回答
我遇到了麻烦,让它与背景颜色和附属视图一起在单元格中工作。最后不得不:
1)使用带有背景颜色的UIView设置单元格的背景视图属性。
let view = UIView()
view.backgroundColor = UIColor.white
self.backgroundView = view
2)在layoutSubviews中重新定位这个视图,添加间距的想法
override func layoutSubviews() {
super.layoutSubviews()
backgroundView?.frame = backgroundView?.frame.inset(by: UIEdgeInsets(top: 2, left: 0, bottom: 0, right: 0)) ?? CGRect.zero
}
其他回答
我的情况是我使用自定义UIView viewForHeader在节也hightforheader在节返回常量高度说40,问题是当没有数据时,所有的头视图被彼此触摸。所以我想在没有数据的部分之间留出空间,所以我通过将“tableview style”平面改为“Group”来固定。这对我很有效。
是的,你可以增加或减少间距(填充)之间的两个单元格通过创建一个基本视图的内容视图在单元格。为内容视图背景设置清晰的颜色,您可以调整基本视图的高度以创建单元格之间的空间。
我使用Swift的简单解决方案:
// Inside UITableViewCell subclass
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
}
结果
为了增加行之间的间距,将使用第二个原型单元格。
在第二个原型单元格中添加一个不同的单元格标识符,然后添加一个空视图,并将其边缘约束到单元格的边缘,然后将emptyView的颜色设置为“clear”。
然后在代码中:
import UIKit
class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var yourTableView: UITableView!
let yourArray = ["data1", "data2", "data3" /* so on */ ]
let emptyTransparentRowHeight: CGFloat = 10
override func viewDidLoad() {
super.viewDidLoad()
yourTableView.dataSource = self
yourTableView.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (yourArray.count * 2)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.row % 2 == 0,
let usableCell = tableView.dequeueReusableCell(withIdentifier: "Your First Cell Prototype Identifier") as? YourTableViewCellSubClass
else {
return tableView.dequeueReusableCell(withIdentifier: "The Second and Empty Cell Prototype Identifier")!
}
usableCell.Label?.text = yourArray[indexPath.row/2]
return usableCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row % 2 == 0 {
return tableView.rowHeight
}
return self.emptyTransparentRowHeight
}
}
斯威夫特版本
Swift 3更新
为了方便以后的观众,这个答案比原来的问题更一般。它是Swift基本UITableView示例的补充示例。
概述
基本思想是为每个数组项创建一个新节(而不是新行)。然后,可以使用节头高度对节进行间隔。
怎么做
按照UITableView示例中的描述设置你的项目。(也就是说,添加一个UITableView并将tableView outlet连接到视图控制器)。 在Interface Builder中,将主视图背景颜色更改为浅蓝色,并将UITableView背景颜色更改为clear。 用以下代码替换ViewController.swift代码。
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// These strings will be the data for the table view cells
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellReuseIdentifier = "cell"
let cellSpacingHeight: CGFloat = 5
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// These tasks can also be done in IB if you prefer.
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
tableView.delegate = self
tableView.dataSource = self
}
// MARK: - Table View delegate methods
func numberOfSections(in tableView: UITableView) -> Int {
return self.animals.count
}
// There is just one row in every section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Set the spacing between sections
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return cellSpacingHeight
}
// Make the background color show through
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear
return headerView
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// note that indexPath.section is used rather than indexPath.row
cell.textLabel?.text = self.animals[indexPath.section]
// add border and color
cell.backgroundColor = UIColor.white
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8
cell.clipsToBounds = true
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// note that indexPath.section is used rather than indexPath.row
print("You tapped cell number \(indexPath.section).")
}
}
注意indexPath。section而不是indexPath。行,以获得数组元素的正确值和点击位置。
你是如何在右边和左边得到额外的填充/空间的?
我得到它的方法和你给任何视图加空格的方法是一样的。我使用了自动布局约束。只需使用接口构建器中的引脚工具为开头和结尾约束添加间距。