ios xcode 8 swift 3 UItableview 单元格之间的空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43849297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:07:05  来源:igfitidea点击:

xcode 8 swift 3 UItableview space between cells?

iosiphonexcodeuitableview

提问by Hyman K Fouani

i am trying to make space between cells in UItableviewi've checked google all posts i found is more than 3 years old , when i try to apply them i am getting so many errors, is it possible to make space between cells in UItableview?

我试图在单元格之间留出空间UItableview我已经检查了谷歌我发现的所有帖子都超过 3 年了,当我尝试应用它们时,我遇到了很多错误,是否可以在单元格之间留出空间UItableview

my Code :

我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell
    cell.model = self.modelAtIndexPath(indexPath)

    let url = URL(string: cell.model.imageName)
    cell.imgBack.kf.setImage(with: url)

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    //send back playlist count
    if (section == 0) {
        return self.lists_arr.count
    }
    return 0
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated:true)
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let imageCell = cell as! ImageCell
    self.setCellImageOffset(imageCell, indexPath: indexPath)
}

//number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}


func modelAtIndexPath(_ indexPath: IndexPath) -> CellPlaylist {
    return self.lists_arr[(indexPath as NSIndexPath).row % self.lists_arr.count]
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (scrollView == self.tblMain) {
        for indexPath in self.tblMain.indexPathsForVisibleRows! {
            if let imgCel : ImageCell = self.tblMain.cellForRow(at: indexPath) as? ImageCell {
             self.setCellImageOffset(imgCel, indexPath: indexPath)
            }
        }
    }
}

func setCellImageOffset(_ cell: ImageCell, indexPath: IndexPath) {
    let cellFrame = self.tblMain.rectForRow(at: indexPath)
    let cellFrameInTable = self.tblMain.convert(cellFrame, to:self.tblMain.superview)
    let cellOffset = cellFrameInTable.origin.y + cellFrameInTable.size.height
    let tableHeight = self.tblMain.bounds.size.height + cellFrameInTable.size.height
    let cellOffsetFactor = cellOffset / tableHeight
    cell.setBackgroundOffset(cellOffsetFactor)

}

The class of TableCell :

TableCell 的类:

class ImageCell: UITableViewCell {

    @IBOutlet weak var lblTitle: UILabel!
    @IBOutlet weak var imgBack: UIImageView!
    @IBOutlet weak var imgBackTopConstraint: NSLayoutConstraint!
    @IBOutlet weak var imgBackBottomConstraint: NSLayoutConstraint!

    let imageParallaxFactor: CGFloat = 70

    var imgBackTopInitial: CGFloat!
    var imgBackBottomInitial: CGFloat!


    var model: CellPlaylist! {
        didSet {
            self.updateView()
        }
    }

    override func awakeFromNib() {
        self.clipsToBounds = true
        self.imgBackBottomConstraint.constant -= 2 * imageParallaxFactor
        self.imgBackTopInitial = self.imgBackTopConstraint.constant
        self.imgBackBottomInitial = self.imgBackBottomConstraint.constant
    }

    func updateView() {
       // self.imgBack.imageFromServerURL(urlString: self.model.imageName)
        //self.getImage(url: self.model.imageName,imgView: self.imgBack)
        //self.model.imageName
        self.layer.cornerRadius = 10
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 8
        self.layer.shadowOffset = CGSize(width: 0, height: 3)
        self.layer.shadowRadius = 3
        self.layer.shadowOpacity = 0.3
        self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 8, height: 8)).cgPath
      self.layer.shouldRasterize = true
     self.layer.rasterizationScale = UIScreen.main.scale

        self.contentView.layoutMargins.top = 20

        self.lblTitle.text = self.model.title
    }

    func setBackgroundOffset(_ offset:CGFloat) {
        let boundOffset = max(0, min(1, offset))
        let pixelOffset = (1-boundOffset)*2*imageParallaxFactor
        self.imgBackTopConstraint.constant = self.imgBackTopInitial - pixelOffset
        self.imgBackBottomConstraint.constant = self.imgBackBottomInitial + pixelOffset
    }



}

The storyboard :

故事板:

enter image description here

在此处输入图片说明

what i am getting

我得到了什么

:

what i want :

我想要的是 :

回答by Silversky Technology

Make Tablview BG colour white and set BG View colour in cell that you want to keep see in image i have set bgview BG colour is light greay and make BGview hight smaller then cell i mean if you want to set space 10 pixel keep Bg hight smaller then cell 10 pixel

使 Tablview BG 颜色为白色并在要保持在图像中看到的单元格中设置 BG 视图颜色然后单元格 10 像素

Here is my cell enter image description hereAnd here is out put

这是我的手机 在此处输入图片说明这是输出

enter image description here

在此处输入图片说明

回答by michalmichalek

Swift 4.2 solution

Swift 4.2 解决方案

Inside UITableViewCell subclass

在 UITableViewCell 子类中

override func layoutSubviews() {
    super.layoutSubviews()

    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = bounds.inset(by: padding)
}

Swift 5.0 solution

Swift 5.0 解决方案

Inside UITableViewCell subclass

在 UITableViewCell 子类中

override func layoutSubviews() {
    super.layoutSubviews()
    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = UIEdgeInsetsInsetRect(bounds, padding)
}

回答by Gulfam Khan

Update: UIEdgeInsetsInsetRect method is replaced now you can do it like this

更新: UIEdgeInsetsInsetRect 方法被替换了,现在你可以这样做

contentView.frame = contentView.frame.inset(by: margins)

Swift 4 answer:

斯威夫特 4 回答:

in your custom cell class add this function

在您的自定义单元类中添加此功能

override func layoutSubviews() {
    super.layoutSubviews()
    //set the values for top,left,bottom,right margins
    let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, margins)
}

You can change values as per your need

您可以根据需要更改值

回答by aircraft

You can let every cell to take a section in group tableView, then you set the section's footer.

您可以让每个单元格在组 tableView 中占据一个部分,然后设置该部分的页脚。

Try my code:

试试我的代码:

import UIKit


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        tableView.register(UINib.init(nibName: "VC1Cell", bundle: nil), forCellReuseIdentifier: "VC1Cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: -- tableview delegate

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let view:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height: 10))
        view.backgroundColor = .clear

        return view
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:VC1Cell = tableView.dequeueReusableCell(withIdentifier: "VC1Cell") as! VC1Cell
        return cell
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10.0
    }


}

The result:

结果:

回答by Allison McEntire

Building on Gulfam Khan's answer, the new way of setting the inset:

基于Gulfam Khan的回答,设置插图的新方法:

Swift 4:

斯威夫特 4:

 override func layoutSubviews() {
        super.layoutSubviews()
        //set the values for top,left,bottom,right margins
        let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
        contentView.frame = contentView.frame.inset(by: margins)
    }

回答by Riyan Fransen

Swift 4:

斯威夫特 4:

override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0))
}

回答by Umer Khan

Swift 5.2 Solution

Swift 5.2 解决方案

override func layoutSubviews() {
    super.layoutSubviews()
    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = bounds.inset(by: padding)
}