我想在每两个表格 View 单元格之间获得分隔线,但不想创建我不需要的额外部分。
例如:
最佳答案
- 创建自定义
TableViewCell
。 - 在垂直
StackView
中包含您需要的 View 。 - 在
StackView
的底部添加一个UIView
并将高度constraint
设置为您需要的空白大小(即 10px) . - 将 StackView 的高度限制设置为所需的单元格高度(即 70 像素)
- 创建对
StackView
高度Constraint
的引用
class CustomCell: UITableViewCell {
@IBOutlet weak var cellLabel: UILabel!
@IBOutlet weak var bottomSpace: UIView!
@IBOutlet weak var stackViewHeight: NSLayoutConstraint!
}
在 viewDidLoad:
tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.separatorStyle = .none
在您的cellForRowAtIndexPath
中:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as? CustomCell else { return UITableViewCell() }
if indexPath.row % 2 == 0 {
cell.bottomSpace.isHidden = true
cell.stackViewHeight.constant = 70.0 //Normal cell height
} else {
cell.bottomSpace.isHidden = false
cell.stackViewHeight.constant = 80.0 //Cell height + whitespace
}
cell.cellLabel.text = data[indexPath.row]
return cell
}
关于ios - 如何在 TableView 单元格之间进行分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202588/