Ios 如何在UITableViewCells之间添加间距-Swift
我有一张定制的桌子 这是我的密码:Ios 如何在UITableViewCells之间添加间距-Swift,ios,swift,uitableview,Ios,Swift,Uitableview,我有一张定制的桌子 这是我的密码: import UIKit class ViewController: UIViewController, UITableViewDelegate { var exercises : [String] = ["Swimming", "Running", "Weight Lifting", "Biking", "Climbing"] override func viewDidLoad() { super.viewDidLoad
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
var exercises : [String] = ["Swimming", "Running", "Weight Lifting", "Biking", "Climbing"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Necessary for basic tableView setup. Defines number of rows in a specific section.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
//Setting the amount of rows to the number of elements in exercises. This function returns that.
tableView.backgroundColor = UIColor.clearColor()
return exercises.count
}
//Necessary for basic tableView setup. Helps us out content for every cell in the index path. Runs = rows
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
tableView.separatorColor = UIColor.clearColor()
//Setting the footer to default so the extra junk does not show
tableView.tableFooterView = UIView()
//This will be returned. This automatically creates a prototype cell
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
//Setting every cell to the respective item in exercises
cell.textLabel?.text = exercises[indexPath.row]
cell.textLabel?.font = UIFont(name: "Avenir-Light", size: 17)
cell.textLabel?.textColor = UIColor.whiteColor()
cell.textLabel?.textAlignment = .Center
//Border Code
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.whiteColor().CGColor
//Round Corners
cell.layer.cornerRadius = 20
return cell
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor.clearColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我希望每个UITableViewCell
之间有一些间距。我已经尝试了以下方法:
heightForHeader.
。我希望避免使用此选项,因为我必须将所有行转换为节谢谢首先,您应该将与tableView相关的代码移出
tableView:cellForRowAtIndexPath
,最好是viewDidLoad
:
override func viewDidLoad {
super.viewDidLoad()
tableView.separatorColor = UIColor.clearColor()
tableView.tableFooterView = UIView()
}
其次,UITableViewCell是可重用的对象,因此在需要时,tableView会将它们排在队列中:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell")
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
...
}
至于您的问题,您应该在tableView上设置行高
override func viewDidLoad {
super.viewDidLoad()
...
tableView.rowHeight = 100.0
}
或者改为实施tableView:heightforrow索引路径:
:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100.0
}
还应更新textLabel的边框和角半径值,而不是单元格:
//Border Code
cell.textLabel.layer.borderWidth = 2.0
cell.textLabel.layer.borderColor = UIColor.whiteColor().CGColor
//Round Corners
cell.textLabel.layer.cornerRadius = 20
我尝试了ozgur的方法,但没有成功,因为我的表视图单元格之间有边界。最后,我使用了来自的答案。希望有帮助您可以通过在单元格内创建一个额外的视图来添加间距,该视图包含顶部和底部之间有间距的单元格内容。使单元格的背景色半透明,它将显示为单元格上下都有间距