如何在TableView单元格之间进行分离

时间:2018-05-06 17:28:08

标签: ios swift uitableview cell

我希望在每两个表视图单元格之间划分分隔线,但不要创建我不需要的额外部分。

例如:

enter image description here

3 个答案:

答案 0 :(得分:4)

  1. 创建自定义TableViewCell
  2. 在垂直StackView
  3. 中包含所需的观看次数
  4. UIView底部添加StackView,并将高度constraint设置为您需要的空白大小(即10px)。
  5. 将StackView高度的约束设置为所需的单元格高度(即70px)
  6. 创建对StackView身高Constraint
  7. 的引用
    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

答案 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))
    }

或者您可以更改故事板的偏移量