2017-05-08 62 views
2

我正在努力使单元格之间的空间在UItableview我已经检查谷歌所有帖子我发现是3岁以上,当我尝试应用他们我得到这么多的错误,是否有可能使单元格之间的空间UItableviewxcode 8 swift 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 
    } 



} 

,故事情节:

enter image description here

什么我得到

enter image description here

我想要什么:

enter image description here

+0

变化'view' UITableCell' – Dhiru

+0

的'了'contentView'内部框架有每一个细胞是属于单独部分的一部分。并给每个部分一个透明的footerView。或者在你的单元格内创建一个视图,并让单元格contentView透明。创建的视图对具有所需间距的contentView有一个底部约束。因为我正在下班,所以无法给出完全成熟的答案。如果你不能工作出来提及它,我会看到关于帮助你以后 – DatForis

+0

@Dhiru我这样做仍然没有间距 –

回答

5

让Tablvi新的BG颜色白色和设置BG查看颜色的单元格,你想保持在图像中看到我已经设置bgview BG颜色是浅灰色,使BGVIEW高度更小,然后单元格我的意思是如果你想设置空间10像素保持Bg高度小于电池10像素

这里是我的手机 enter image description here ,这里是出把

enter image description here

+0

这比添加部分更有意义... –

4

你可以让每一个细胞都采取的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 
    } 


} 

结果:

相关问题