UICollectionViewCell在滚动时重新排序或消失

时间:2017-04-03 14:14:01

标签: ios swift uicollectionview uicollectionviewcell

我遇到UICollectionView问题。我使用FlowLayout创建了一个集合,我试图仅调整第二个项目的大小,使其宽度为2xcellWidth。当View出现时没关系,但在此之后,当我滚动并返回顶部时,我的单元格会消失,重新排序或更改它的宽度。这完全是一种奇怪的表现。 所以要清楚:我创建了集合视图,滚动到底部,当我回到顶部时,我看到我的单元格在这样一个奇怪的地方:

之前: enter image description here

后: enter image description here

class MapEventsViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {

    var collectionView: UICollectionView!
    let screenWidth = UIScreen.main.bounds.width

    let options = [Options(id: "", value: "28-03-2017 10:54:01"),
                   Options(id: "", value: "Berlin Strasse 6A 00-000, Berlin", isDoubled: true),
                   Options(id: "Status", value: "-"),
                   Options(id: "Prędkość", value: "40km/h"),
                   Options(id: "Licznik", value: "250000km"),
                   Options(id: "Napięcie", value: "15V")]

    override func viewDidLoad() {
        super.viewDidLoad()

        view.frame.size.height = 170

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()   

        layout.sectionInset = UIEdgeInsets(top: 1, left: 0, bottom: 0, right: 1)
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(MapEventCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView.backgroundColor = UIColor.white
        collectionView.isScrollEnabled = true
        collectionView.alwaysBounceVertical = true
        collectionView.autoresizingMask = UIViewAutoresizing.flexibleWidth
        collectionView.backgroundColor = UIColor.init(white: 0.90, alpha: 1.0)
        collectionView.showsVerticalScrollIndicator = true


        self.view.addSubview(collectionView)

    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return options.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MapEventCell

        cell.awakeFromNib()

        cell.id.font = UIFont.systemFont(ofSize: 12)
        cell.value.font = UIFont.boldSystemFont(ofSize: 14)

        cell.backgroundColor = UIColor.white

        if indexPath.row == 0{
            cell.value.font = UIFont.systemFont(ofSize: 10)
            cell.value.frame.origin.y -= 6
        }

        if indexPath.row == 1{
            cell.value.font = UIFont.systemFont(ofSize: 10)
            cell.value.frame.origin.y -= 6
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let thisCell = cell as! MapEventCell

        thisCell.id.text = options[indexPath.item].id
        thisCell.value.text = options[indexPath.item].value
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        print("sizeForItemAt")
        let cellWidth = UIScreen.main.bounds.width / 3 - 1
        if options[indexPath.item].isDoubled{
            return CGSize(width: cellWidth * 2 + 1, height: 40)
        }
        return CGSize(width: cellWidth, height: 40)
    }
}

class MapEventCell: UICollectionViewCell{

    var id: UILabel = UILabel()
    var value: UILabel = UILabel()

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

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        print("addViews")

        id.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: 16)
        id.textAlignment = .center
        value.frame = CGRect(x: 0, y: 16, width: contentView.frame.width, height: 20)
        value.textAlignment = .center

//        id.font = UIFont.systemFont(ofSize: 12)
        id.textColor = UIColor.lightGray

//        value.font = UIFont.boldSystemFont(ofSize: 14)

        contentView.addSubview(id)
        contentView.addSubview(value)

    }

    override func prepareForReuse() {
        id.removeFromSuperview()
        value.removeFromSuperview()
    }

}

更新 我通过计算CellForItemAt indexPath中单元格中每个元素的帧来解决这个问题。所以现在它看起来像这样(并且有效):

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MapEventCell
    cell.awakeFromNib()
    cell.backgroundColor = UIColor.white

    if indexPath.section == 0{
        cell.value.frame = CGRect(x: 0, y: 0, width: (screenWidth / 3), height: 30)
        if options[indexPath.item].isDoubled {
            cell.value.frame = CGRect(x: 0, y: 0, width: (screenWidth / 3) * 2 - 2, height: 30)
            cell.value.textAlignment = .center
        }
        cell.id.isHidden = true
        cell.value.font = UIFont.systemFont(ofSize: 10)
    }else{
        //cell.frame = CGRect(x: 0, y: 0, width: (screenWidth / 3) - 1, height: 40)
        cell.id.isHidden = false
        cell.id.font = UIFont.systemFont(ofSize: 10)
        cell.value.font = UIFont.boldSystemFont(ofSize: 12)

    }

    return cell
}

但现在我不确定它是否很好解决。我知道每次细胞都在重复使用,所以我的问题是,这个问题有更好的解决方案吗?

0 个答案:

没有答案