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

标签 ios iphone xcode uitableview

我试图在 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
    }



}

Storyboard:

enter image description here

what i am getting

:

what i want :

最佳答案

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

这是我的手机 enter image description here 这是输出

enter image description here

关于ios - xcode 8 swift 3 单元格之间的 UItableview 空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849297/

相关文章:

ios - 无法读取 PLugin.Ble 中的特征

javascript - 防止移动设备上的标准选择行为

iOS 应用程序崩溃,xcode 在调试时显示 'Lost connection to X' s iPhone'

iphone - 我没有影子

ios - 从 iOS 调用 WCF 服务时出现什么问题

iphone - iOS/Mac 开发,使用 CFilter 的问题

iphone - 如何按特定日期打开和阻止 View Controller

ios - 使核心数据线程安全

ios - 我需要推送通知来提醒用户某事吗

xcode - iOS 7背景音效