ios - 如何在 TableView 单元格之间进行分隔

标签 ios swift uitableview cell

我想在每两个表格 View 单元格之间获得分隔线,但不想创建我不需要的额外部分。

例如:

enter image description here

最佳答案

  1. 创建自定义 TableViewCell
  2. 在垂直 StackView 中包含您需要的 View 。
  3. StackView 的底部添加一个 UIView 并将高度 constraint 设置为您需要的空白大小(即 10px) .
  4. 将 StackView 的高度限制设置为所需的单元格高度(即 70 像素)
  5. 创建对 StackView 高度 Constraint 的引用

class CustomCell: UITableViewCell {

    @IBOutlet weak var cellLabel: UILabel!
    @IBOutlet weak var bottomSpace: UIView!
    @IBOutlet weak var stackViewHeight: NSLayoutConstraint!

}

enter image description here

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
    }

enter image description here

关于ios - 如何在 TableView 单元格之间进行分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202588/

相关文章:

android - 用于 Android 和 iOS 应用程序的 WebSockets

iOS UiLocalNotification 格式化

ios - 如何从 Apple Watch 中的模态转场更改标签 "Cancel"

swift - 调用完成处理程序后返回,还是返回完成处理程序参数?

iphone - UITableView titleForSection 字体

ios - 如何为多个设备实现核心蓝牙功能?

arrays - Swift 5 结构数组中的嵌套排序

ios - 使图像适合圆形 ImageView

objective-c - UITableView setEditing 为 NO 不会改变 barButtonItem

iOS tableview 在将图像加载到 imageview 时滞后于滚动