【问题标题】:xcode 8 swift 3 UItableview space between cells?xcode 8 swift 3 单元格之间的 UItableview 空间?
【发布时间】:2017-10-06 13:17:03
【问题描述】:

我正在尝试在UItableview 中的单元格之间留出空间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
    }



}

故事板:

我得到了什么

:

我想要什么:

【问题讨论】:

  • 在UITableCell的contentView内改变view的框架
  • 让每个单元格成为单独部分的一部分。并给每个部分一个透明的footerView。或者在您的单元格内创建一个视图并使单元格 contentView 透明。创建的视图将对具有所需间距的 contentView 具有底部约束。无法给出完全成熟的答案,因为我正在下班的路上。如果您无法解决,请提出,我稍后会帮助您
  • @Dhiru 我这样做了仍然没有间距
  • @DatForis :) 谢谢我会等你完整的答复

标签: ios iphone xcode uitableview


【解决方案1】:

使 Tablview BG 颜色为白色并在要保留在图像中看到的单元格中设置 BG View 颜色我已将 bgview BG 颜色设置为浅灰色并使 BGview 高度比单元格更小我的意思是如果你想设置空间 10 像素保持背景高度比单元格小 10 像素

这是我的牢房 这里是放出来的

【讨论】:

  • 谢谢。比添加部分要好。
【解决方案2】:

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

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

您可以根据需要更改值

【讨论】:

  • 设置插图的旧方法 - 已过时
  • @cfl 新方法是什么?
  • @UtkuDalmaz 当我在 Xcode 中输入它时,它显示为已过时。我还没想出解决办法
  • 如果您能分享更新的操作方法,我将不胜感激。我做了一些研究,但没有发现更好的
【解决方案3】:

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

Swift 5.0 解决方案

在 UITableViewCell 子类中

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

【讨论】:

    【解决方案4】:

    您可以让每个单元格在 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
        }
    
    
    }
    

    结果:

    【讨论】:

    • 但是你在这个实现中有 10 个部分。这是一种解决方法,在单元格内使用布局。
    • @LoraKucher 在单元格内使用布局也是解决方法
    【解决方案5】:

    以Gulfam Khan 的回答为基础,设置插图的新方法:

    斯威夫特 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)
        }
    

    【讨论】:

      【解决方案6】:

      斯威夫特 4:

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

      【讨论】:

        【解决方案7】:

        Swift 5.2 解决方案

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

        【讨论】:

          猜你喜欢
          • 2012-03-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多