UICollectionView在滚动时显示奇怪的行为

时间:2018-02-26 06:45:03

标签: ios swift4 uicollectionviewlayout

我正在尝试创建seatMap,我想隐藏某个单元格的图像,但是当我这样做时,图像隐藏在其他单元格中随机不会出现问题。假设我隐藏了第二个单元格的图像,当我水平或垂直滚动​​时,第四个单元格的图像也隐藏了。

这是我的ViewController代码

import UIKit 
class BookViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var dropButton: UIButton!
@IBOutlet weak var dropDownOutlet: UITableView!
@IBOutlet weak var seatMapCollection: UICollectionView!
@IBOutlet weak var dateSelecterCollection: UICollectionView!
@IBOutlet weak var timeSelecterCollection: UICollectionView!


var selectedIndexPath: IndexPath!
var dateValue = ["1","2","3","4","10","6","7","8","9","5"]
var dayValue = ["Mon","Tue","Wed","Thu","Thu","Thu","Thu","Thu","Thu","Thu"]
var month = ["jan","feb","mar","apr","Thu","Thu","Thu","Thu","Thu","Thu"]
var venue = ["Smart Cinemaz, Rupnagar","Smart Cinemaz, Fatehgarh Sahib "]
var numberSection:Int = 10
var numberOfItems:Int = 15
var seatNumber = [Int]()
var columns = [[Int]]()
var rows = [[Int]]()
var col = [2,3]
var rowno = [6,7,14]
var col1 = [5,6,7,8,9,11,12]
var rowno1 = [7]
var sectionHeader = [0,4,10]

override func viewDidLoad() {
    super.viewDidLoad()
    self.seatMapCollection.dataSource = self
    self.seatMapCollection.delegate = self
    self.seatMapCollection.reloadData()
    self.seatMapCollection.maximumZoomScale = 2
    self.seatMapCollection.minimumZoomScale = 0.50
    self.view.sendSubview(toBack: seatMapCollection)
    seatMapCollection.register(UINib(nibName: "HeaderMulticolumn", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
    for seat in 0...numberOfItems-1{

        seatNumber.append(seat + 1)

    }
    self.dateSelecterCollection.dataSource = self
    self.dateSelecterCollection.delegate = self

    self.timeSelecterCollection.dataSource = self
    self.timeSelecterCollection.delegate = self
    self.dropDownOutlet.delegate = self
    self.dropDownOutlet.dataSource = self
    dropButton.contentHorizontalAlignment = .left


   /* let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
    seatMapCollection.addGestureRecognizer(pinchGesture)*/

    columns.append(col)
    columns.append(col1)
    rows.append(rowno)
    rows.append(rowno1)
}




@IBAction func dropDown(_ sender: Any) {
    self.view.bringSubview(toFront: dropDownOutlet)
    if dropDownOutlet.isHidden == true{
        dropDownOutlet.isHidden = false
    }else {
        dropDownOutlet.isHidden = true
    }
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView.tag == 1{
        return 1
    }else if collectionView.tag == 2{
        return 1
    }else{

        return 15

    }
}


func numberOfSections(in collectionView: UICollectionView) -> Int {
    if collectionView.tag == 1{
        return 7
    }else if collectionView.tag == 2{
        return 5
    }

    else{

        return 10 + 3

    }

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView.tag == 1 {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DateSelecterCollectionViewCell", for: indexPath) as! DateSelecterCollectionViewCell
        if (self.selectedIndexPath != nil && indexPath == self.selectedIndexPath){
            cell.backgroundColor = UIColor.yellow
            cell.dateOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
            cell.monthOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
            cell.dayOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
        }
        else{
            cell.backgroundColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
            cell.dateOutlet.textColor = UIColor.white
            cell.monthOutlet.textColor = UIColor.white
            cell.dayOutlet.textColor = UIColor.white
        }
        return cell

    }else if collectionView.tag == 2{
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TimeSelecterCollectionViewCell", for: indexPath) as! TimeSelecterCollectionViewCell
        if (self.selectedIndexPath != nil && indexPath == self.selectedIndexPath){
            cell.backgroundColor = UIColor.yellow
            cell.timeOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)

        }
        else{
            cell.backgroundColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
            cell.timeOutlet.textColor = UIColor.white
        }
        return cell
    }

    else {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SeatMapCollectionViewCell", for: indexPath) as! SeatMapCollectionViewCell

        for r1 in 0...rows.count-1{
            for c1 in 0...columns.count-1{
                if (r1 == c1){

                    for colum in 0...columns[c1].count-1 {
                        for ro in 0...rows[r1].count-1{
                            if indexPath.row == rows[r1][ro] && indexPath.section == columns[c1][colum]{
                                cell.alpha = 0


                            }
                        }
                    }
                }

            }
        }
        for i in 0...sectionHeader.count-1{
            if indexPath.section==sectionHeader[i] && indexPath.row==0{
                cell.seatDetail.alpha=1
                cell.seatDetail.text = "Gold - 180"
                cell.seatImage.isHidden = true
            }
        }

       //cell.seatDetail.alpha=0

        return cell


    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView.tag == 1{
        let cell = collectionView.cellForItem(at: indexPath) as! DateSelecterCollectionViewCell
    if cell.isSelected == true{
    cell.backgroundColor = UIColor.yellow
        cell.dateOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
        cell.monthOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
        cell.dayOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
    }


    self.selectedIndexPath = indexPath
    }else if collectionView.tag == 2 {
        let cell = collectionView.cellForItem(at: indexPath) as! TimeSelecterCollectionViewCell
        self.selectedIndexPath = nil
        cell.backgroundColor = UIColor.yellow
        cell.timeOutlet.textColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
    }

}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    if collectionView.tag == 1{
    let cell = collectionView.cellForItem(at: indexPath) as! DateSelecterCollectionViewCell
    self.selectedIndexPath = nil
    cell.backgroundColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
    cell.dateOutlet.textColor = UIColor.white
    cell.monthOutlet.textColor = UIColor.white
    cell.dayOutlet.textColor = UIColor.white

    }
    else if collectionView.tag == 2{
        let cell = collectionView.cellForItem(at: indexPath) as! TimeSelecterCollectionViewCell
        self.selectedIndexPath = nil
        cell.backgroundColor = UIColor(red: 44/255, green: 150/255, blue: 208/255, alpha: 1)
        cell.timeOutlet.textColor = UIColor.white

    }

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return venue.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DropDownTableViewCell") as! DropDownTableViewCell
    cell.venueDetails.text = "\(venue[indexPath.row])"
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! DropDownTableViewCell
    dropButton.setTitle(cell.venueDetails.text, for: .normal)
    tableView.isHidden = true
}



}

这是我的Custom CollectionViewlayout

   import UIKit 
   protocol SeatMapDelegate:class {

func collectionView(Width: Double)-> Double
func collectionView(Height: Double)-> Double }

  class CustomCollectionViewLayout: UICollectionViewLayout {
var delegate: SeatMapDelegate?
var CELL_HEIGHT = 35.0
var CELL_WIDTH = 35.0
let STATUS_BAR = UIApplication.shared.statusBarFrame.height

var cache = [UICollectionViewLayoutAttributes]()
var contentSize = CGSize.zero
var cellAttrsDictionary = [UICollectionViewLayoutAttributes]()
var cellPadding = 5.0
var sectionHeader = [0,4,10]

override var collectionViewContentSize: CGSize{

    return self.contentSize
}
var interItemsSpacing: CGFloat = 8

// 5
var contentInsets: UIEdgeInsets {
    return collectionView!.contentInset
}
override func prepare() {
    var counts: Int = 0

    // Cycle through each section of the data source.
    if collectionView!.numberOfSections > 0 {
        for section in 0...collectionView!.numberOfSections-1 {

            // Cycle through each item in the section.
            if collectionView!.numberOfItems(inSection: section) > 0 {
                for item in 0...collectionView!.numberOfItems(inSection: section)-1 {

                    // Build the UICollectionVieLayoutAttributes for the cell.
                    let cellIndex = NSIndexPath(item: item, section: section)
                   /* let width: Double = 35.0
                    let height: Double = 35.0
                    CELL_WIDTH = (delegate?.collectionView(Width: width))!
                    CELL_HEIGHT = (delegate?.collectionView(Height: height))!*/
                    if counts<sectionHeader.count {
                        if item == 0 && section == sectionHeader[counts]{
                            let xPos = 0.0
                            let yPos = Double(section) * CELL_HEIGHT
                            let frame = CGRect(x: xPos, y: yPos, width: 15 * CELL_WIDTH, height: CELL_HEIGHT)
                            let cellFinalAttribute = frame.insetBy(dx:CGFloat(cellPadding) ,dy:CGFloat(cellPadding))
                            let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex as IndexPath)
                            cellAttributes.frame = cellFinalAttribute
                            //cellAttributes.frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT)

                            cellAttrsDictionary.append(cellAttributes)

                            counts=counts+1

                            break

                        }

                    }







                    let xPos = Double(item) * CELL_WIDTH
                    let yPos = Double(section) * CELL_HEIGHT
                    let frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT)
                    let cellFinalAttribute = frame.insetBy(dx:CGFloat(cellPadding) ,dy:CGFloat(cellPadding))
                    let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex as IndexPath)
                    cellAttributes.frame = cellFinalAttribute
                    //cellAttributes.frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT)

                    cellAttrsDictionary.append(cellAttributes)






                }

            }

        }
    }

    // Update content size.
    let contentWidth = Double(collectionView!.numberOfItems(inSection: 0)) * CELL_WIDTH
    let contentHeight = Double(collectionView!.numberOfSections) * CELL_HEIGHT
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)

}


override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {



    // Create an array to hold all elements found in our current view.
    var attributesInRect = [UICollectionViewLayoutAttributes]()

    // Check each element to see if it should be returned.
    for cellAttributes in cellAttrsDictionary {
       if rect.intersects(cellAttributes.frame) {
            attributesInRect.append(cellAttributes)
        }

    }

    // Return list of elements.
    return attributesInRect
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

    return cellAttrsDictionary[indexPath.row]
}

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
    return false
}

}

自定义CollectionView单元类

import UIKit
 class SeatMapCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var seatDetail: UILabel!

@IBOutlet weak var seatImage: UIImageView!
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    setup()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setup()
}

func setup() {
    //self.layer.borderWidth = 1.0
   // self.layer.borderColor = UIColor.gray.cgColor
   // self.layer.cornerRadius = 5.0

}}

0 个答案:

没有答案