Swift为UITableVIew添加边距

时间:2015-10-14 05:11:52

标签: swift uitableview swift2

read the article关于向UIViewTable添加边距,但代码是Object-C,我想要Swift中的示例。我想创建一个像这样的UIViewTable:

enter image description here

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

您可以像平常一样使用表格视图,只需在tableView单元格中添加UIView,并在图像中附加约束。然后,您可以在UIView中添加所有控件,还可以设置tableview和表格视图单元格的背景颜色以清除颜色。

enter image description here

你会得到这个输出

enter image description here

由于

答案 1 :(得分:1)

按照他在回答中提到的步骤进行操作,下面是您的快速代码:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell : UITableViewCell? = nil

    if indexPath.row % 2 == true {
        cell = self.tableView.dequeueReusableCellWithIdentifier("ReuseInset", forIndexPath: indexPath)
        return cell!

    } else {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel!.text = "MMP"
        return cell
    }

}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if indexPath.row % 2 == true {
        return 10
    }

    return 70
}