我希望在每两个表视图单元格之间划分分隔线,但不要创建我不需要的额外部分。
例如:
答案 0 :(得分:4)
TableViewCell
。StackView
。UIView
底部添加StackView
,并将高度constraint
设置为您需要的空白大小(即10px)。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
}
答案 1 :(得分:0)
您可以创建自定义单元格并根据需要添加行
class CustomCell: UITableViewCell {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy public var line : UIView = {
let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0.5))
view.backgroundColor = .gray
return view
}()
lazy public var stackLine : UIStackView = {
let stackLine = UIStackView.init(arrangedSubviews: [self.line,self.line,self.line,self.line,self.line]) // line as you need
stackLine.axis = .vertical
stackLine.distribution = .fill
stackLine.spacing = 1
stackLine.alignment = .fill
return stackLine
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(stackLine)
NSLayoutConstraint.activate([
stackLine.leadingAnchor .constraint(equalTo: self.contentView.leadingAnchor, constant: 0),
stackLine.trailingAnchor .constraint(equalTo: self.contentView.trailingAnchor, constant: 0),
stackLine.bottomAnchor .constraint(equalTo: self.contentView.bottomAnchor, constant: 0),
stackLine.heightAnchor.constraint(equalToConstant: 5 * 0.5 + 1 * 4 ) // say 5 line * height of line + line space * number ofline -1
]
)
}
}
答案 2 :(得分:0)
这样做的一种方法是
// Inside UITableViewCell subclass
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(10, 10, 10, 10))
}
或者您可以更改故事板的偏移量