簡體   English   中英

如何更改TableView單元格之間的空間iOS Swift

[英]How can I change the space between tableview cells ios swift

如何更改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;
    }

要使用該方法,您必須更改邏輯

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
}

使用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)
}

您可以使用布局邊距功能。 僅適用於iOS8。 子類化UITableViewCell和

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

嗨,我發現的最簡單方法就是將其更改為

 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 } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM