本文介绍了xcode 8 swift 3 UItable单元格之间的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UItableview 中的单元格之间留出空间我已经检查了谷歌我找到的所有帖子都超过3年,当我尝试应用它们时我是得到这么多错误,是否可以在 UItableview 中的单元格之间留出空间?



我的代码:

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

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

返回单元格
}

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

//发回播放列表数
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)
}

//表格中的部分数量
func numberOfSections(在tableView中:UITableView) - > Int {
return 1
}


func modelAtIndexPath(_ indexPath:IndexPath) - > CellPlaylist {
返回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! {
如果让imgCel:ImageCell = self.tblMain.cellForRow(at:indexPath)为? 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)

}

班级of TableCell:

  class ImageCell:UITableViewCell {

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

让imageParallaxFactor:CGFloat = 70

var imgBackTopInitial:CGFloat!
var imgBackBottomInitial:CGFloat!


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

覆盖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,byRo undingCorners:.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
}



}

故事板:





解决方案

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



这是我的单元格

此处已完成




i am trying to make space between cells in UItableview i'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 ?

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 :

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 :

:

解决方案

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

Here is my cellAnd here is out put

这篇关于xcode 8 swift 3 UItable单元格之间的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:17