我正在快速创建一个 IOS 应用程序,并希望像这样在单元格之间添加间距
我想给每个表格视图单元格的空间,就像我的附加图像一样。我怎么能这样做?现在所有的细胞都没有任何空间。迅捷3
你可以在你的 tableView 单元格中试试这个:
class cell: UITableViewCell{
override var frame: CGRect {
get {
return super.frame
}
set (newFrame) {
var frame = newFrame
frame.origin.y += 4
frame.size.height -= 2 * 5
super.frame = frame
}
}
}
更新: UIEdgeInsetsInsetRect 方法现在被替换了,你可以这样做
contentView.frame = contentView.frame.inset(by: margins)
斯威夫特 4 答案:
在您的自定义单元类中添加此功能
override func layoutSubviews() {
super.layoutSubviews()
//set the values for top,left,bottom,right margins
let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, margins)
}
您可以根据需要更改值
***** 注意 ***** 调用超级函数
super.layoutSubviews()
非常重要,否则你会遇到奇怪的问题
If you are using UITableViewCell to achieve this kind of layout, there is no provision to provide spacing between UITableViewCells.
Here are the options you can choose:
UIView within UITableViewCell with clear background, so that it appears like the spacing between cells.You need to set the background as clear of: cell, content view.
UICollectionView instead of UITableView. It is much more flexible and you can design it the way you want.Let me know if you want any more details regarding this.
一种简单的方法是集合视图而不是表格视图,并为集合视图提供单元格间距并使用
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let widthSize = collectionView.frame.size.width / 1
return CGSize(width: widthSize-2, height: widthSize+20)
}
如果您只想要 tableview,则将背景视图添加为容器视图,并将背景颜色设置为白色和单元格背景颜色清除颜色设置单元格前导、颤音、底部的背景视图为 10
backgroundView.layer.cornerRadius = 2.0
backgroundView.layer.masksToBounds = false
backgroundView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
请尝试一下。它对我来说很好。
您可以使用部分而不是行。您在方法中返回数组计数并在委托方法numberOfSectionsInTableView中设置 1numberOfRowsInSection
[array objectAtIndex:indexPath.section]在cellForRowAtIndexPath方法中使用。
设置heightForHeaderInSection为 40 或根据您的要求。
谢谢,希望对你有帮助
- 静态设置 UITableViewCell 间距 - Swift 4 - 未完全测试。
将您的 tableView 行高设置为您喜欢的任何值。
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = <Your preferred cell size>
tableView.rowHeight = UITableViewAutomaticDimension
// make sure to set your TableView delegates
tableView.dataSource = self
tableView.delegate = self
}
extension YourClass : UITexFieldDelegate, UITextFieldDataSource {
//Now set your cells.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "yourCell", for: indexPath) as! UITableViewCell
//to help see the spacing.
cell.backgroundColor = .red
cell.textLabel?.text = "Cell"
return cell
}
//display 3 cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
//now lets insert a headerView to create the spacing we want. (This will also work for viewForHeaderInSection)
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//you can create your own custom view here
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 44) //size of a standard tableViewCell
//this will hide the headerView and match the tableView's background color.
view.backgroundColor = UIColor.clear
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
}