如何确定UICollectionView单元格之间的确切间距

时间:2015-11-02 16:45:37

标签: ios swift uicollectionview uicollectionviewcell

我是IOS开发中的新手。我得到了工作来制作日历应用程序。我使用UICollectionViewCell显示1-30天。对于喜欢" S M T W TH F S"我使用补充视图(headerCell)并添加7 UILabels,其SPACING与两个单元格之间的距离相同。我看到Daynames没有与Day Numbers垂直对齐。我已将UICollectionViewDelegateFlowLayout扩展为将minimumInteritemSpacingForSectionAtIndex设置为2,并且我将单元格大小设置为每行仅显示7天(1周)。

我还使用它们的中心计算了2个单元格之间的距离,将该距离作为参考,并将其添加为HeaderCell中标签之间的距离,但它们仍然没有对齐。

我希望精确计算UICollectionView中单元格之间的距离。我们设置的间距只是最小值,有时根据iPhone的屏幕尺寸而有所不同。

这是Pic: missing alignment with day cells 这是我的代码:

    extension FirstViewController: UICollectionViewDelegateFlowLayout {
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            var cwidth:CGFloat = 0.0
            cwidth = (collectionView.frame.size.width/7 - PADDING - SPACING)

            let cheight = collectionView.frame.size.height/7
            cellWidth = cwidth
            cellHeight = cheight
            print("Cell width is \(cellWidth) and view width is \(collectionView.frame.size.width)")
            return CGSize(width: cwidth, height: cheight )

        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
            return UIEdgeInsets(top:1.0,left:1.0,bottom:1.0,right:1.0)
        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 1.0
        }

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
            return SPACING
        }

    }

      class FirstViewController:       
     UIViewController,UICollectionViewDataSource,UICollectionViewDelegate 
    {

     func collectionView(collectionView: UICollectionView,    
      viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath:    
      NSIndexPath) -> UICollectionReusableView 
    {



        let cell = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "headerCell", forIndexPath: indexPath)

        var prevWidth:CGFloat = 5.0

        var frame1 :CGRect? = nil

        for (index,i) in daynames.enumerate() {

            if (index == 0){
            frame1 = CGRect(x: 1.0, y: -2.0, width: cellWidth  , height: cellHeight)
            }
            else{
              frame1 = CGRect(x: CGFloat(prevWidth)  , y: -2.0,width:cellWidth   , height: cellHeight)
            }
            if (index < 6){
            prevWidth = frame1!.origin.x  + cellWidth + distances[index] - SPACING
            }
            let label = UILabel(frame:frame1!)
            label.text = i
            label.textAlignment = NSTextAlignment.Center
            label.textColor = UIColor.whiteColor()
            label.font = UIFont(name:"Helvetica",size:16)
            label.backgroundColor = UIColor.blackColor()
           // label.center.x = cell_centres[index]
            cell.addSubview(label)

        }

        return cell

}
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath     indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        let label = cell.contentView.viewWithTag(1) as! UILabel
        //testing purpose
        label.text = String(days[indexPath.row])

        if indexPath.row == 0{
            cell_centres.removeAll()
        }

        cell.backgroundColor = UIColor.greenColor()

        if indexPath.row < 7{

            cell_centres.append(cell.center.x)

            print("appending \(cell.center.x)")
        }

        //code for calc distances between cells
        if(prevX == 0){
            prevX = cell.center.x
        }
        else if(indexPath.row < 7){
            distance = floor((cell.center.x - prevX) - cellWidth)
            print("distance \(distance) ")
            distances.append(distance)
            prevX = cell.center.x
        }
        return cell

    }
    }

0 个答案:

没有答案