【发布时间】:2019-01-01 00:11:26
【问题描述】:
【问题讨论】:
-
到目前为止你尝试了什么?
-
我不知道我该如何解决这就是为什么要在这里发帖 - dahiya_boy
-
检查我的答案。要获得此演示,请给我您的电子邮件 ID。
标签: ios uitableview swift3 cellspacing
【问题讨论】:
标签: ios uitableview swift3 cellspacing
你可以在你的 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)
Swift 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()
非常重要,否则你会遇到奇怪的问题
【讨论】:
【讨论】:
一种简单的方法是集合视图而不是表格视图,并为集合视图提供单元格间距并使用
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)
}
如果你只想要表格视图,那么添加背景视图作为容器视图并将背景颜色设置为白色和单元格背景颜色清除颜色设置单元格前导、颤音、底部的背景视图为 10
backgroundView.layer.cornerRadius = 2.0
backgroundView.layer.masksToBounds = false
backgroundView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
【讨论】:
请试一试。它对我来说很好用。
您可以使用部分而不是行。
您在numberOfSectionsInTableView 方法中返回数组计数并在numberOfRowsInSection 委托方法中设置1
在cellForRowAtIndexPath 方法中使用[array objectAtIndex:indexPath.section]。
将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
}
}
【讨论】: