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

Ios xcode 8 swift 3 UItableview单元格之间的空间?,ios,iphone,xcode,uitableview,Ios,Iphone,Xcode,Uitableview,我试图在UItableview中的单元格之间留出空间,我在谷歌上查过,我找到的所有帖子都超过3年了,当我尝试应用它们时,我遇到了很多错误,有可能在UItableview中的单元格之间留出空间吗 我的代码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(wi

我试图在
UItableview
中的单元格之间留出空间,我在谷歌上查过,我找到的所有帖子都超过3年了,当我尝试应用它们时,我遇到了很多错误,有可能在
UItableview
中的单元格之间留出空间吗

我的代码:

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)

}
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
    }



}
故事板:

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)

}

我得到了什么

:

我想要的是:


将Tablview BG颜色设置为白色,并在要在图像中看到的单元格中设置BG View颜色我已将bgview BG颜色设置为浅灰色,并使bgview hight小于单元格我的意思是,如果要设置空间10像素,请将BG hight小于单元格10像素

这是我的手机 这是我的


您可以让每个单元格在group tableView中获取一个节,然后设置节的页脚

试试我的代码:

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
    }


}
结果是:


更新:UIEDGEINSETINSETINSERCT方法已被替换,现在您可以这样做

contentView.frame = contentView.frame.inset(by: margins)
Swift 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 = UIEdgeInsetsInsetRect(contentView.frame, margins)
}

您可以根据需要更改值

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

Swift 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)
    }
override func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0))
}
Swift 4.2解决方案 UITableViewCell子类内部

override func layoutSubviews() {
    super.layoutSubviews()

    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = bounds.inset(by: padding)
}
override func layoutSubviews() {
    super.layoutSubviews()
    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = UIEdgeInsetsInsetRect(bounds, padding)
}
Swift 5.0解决方案 UITableViewCell子类内部

override func layoutSubviews() {
    super.layoutSubviews()

    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = bounds.inset(by: padding)
}
override func layoutSubviews() {
    super.layoutSubviews()
    let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    bounds = UIEdgeInsetsInsetRect(bounds, padding)
}
Swift 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)
    }
override func layoutSubviews() {
    super.layoutSubviews()

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

Swift 5.2解决方案

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

UITableCell
contentView
中更改
view
的框架,使每个单元格都成为单独部分的一部分。并为每个部分提供一个透明的页脚视图。或者在单元格内创建视图,并使单元格内容视图透明。创建的视图将对具有所需间距的contentView具有底部约束。不能给一个完全成熟的答案,因为我在路上。如果你不能解决这个问题,就提出来,我会设法帮助你的later@Dhiru我这么做了还是没有spacing@DatForis:)谢谢我会等你完整的回复好的:设置插图的老方法-outdated@cfl新方法是什么?@UtkuDalmaz当我在Xcode中键入它时,它显示为过时。我还没有找到解决办法,如果你能分享最新的方法,雪人会很感激的。我做了一些研究,但没有找到更好的答案。比添加节要好得多。但是在这个实现中,你有10个节。这是一个解决办法,在单元格内使用布局。