如何更改tableview单元格之间的空间ios swift

时间:2015-11-26 09:54:44

标签: ios uitableview swift2

如何更改tableviewcontroller中单元格之间的空间?。我有一个使用Swift的应用程序。我添加了一个tableviewcontroller工作正常。现在我的问题是,如果我试图改变它不工作的单元格之间的空间。我试过以下代码。

 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10; // space b/w cells
    }

请帮助我找出问题所在?

修改1:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        let a = 1;
        return a;
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = itemArray.count;
        return a;
    }

4 个答案:

答案 0 :(得分:2)

要使用此方法,您必须更改逻辑

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    let a = itemArray.count;
    return a;
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let a = 1;
    return a;
}

并且必须使用indexPath.section而不是indexPath.row更改rowAtIndexPath ...,例如

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")

    cell.titleLabel.text = itemArray[indexPath.section] // if itemArray is [String]

    return cell
}

答案 1 :(得分:1)

使用Swift 2,你可以用这种方式在UITableViewCells之间做间隔:

// In this case I returning 140.0. You can change this value depending of your cell
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 140.0
}

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.clearColor()

    let whiteRoundedView : UIView = UIView(frame: CGRectMake(0, 10, self.view.frame.size.width, 120))

    whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 1.0])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)

    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubviewToBack(whiteRoundedView)
}

答案 2 :(得分:0)

您可以使用布局边距功能。这仅适用于iOS8。子类化UITableViewCell和

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsMake(10, 0, 10, 0);
}

答案 3 :(得分:0)

我发现最简单的方法就是改变它 FROM:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        let a = 1
        return a
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = itemArray.count
        return a
    }

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        let a = itemArray.count
        return a
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = 1
        return a
    }

并添加:

override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return heightOfSpaceBetweenCells
    }
    
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerV = UIView()
            footerV.backgroundColor = UIColor.clearColor()

            return footerV
    }