首页 > 解决方案 > 为什么我的 collectionView 中的标签小于给定的 dimesinsons

问题描述

我正在使用 collectionView 制作标签来拖动和重新排序一组数字,以使用户按顺序排列数字。我的代码如下:

import UIKit

class ViewController: UIViewController {
    fileprivate var longPressGesture: UILongPressGestureRecognizer!
    @IBOutlet weak var collectionview: UICollectionView!
    var str_arr = ["1", "3", "4", "5", "2"]
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
        collectionview.addGestureRecognizer(longPressGesture)
        longPressGesture.minimumPressDuration = 0.5
    }
    
    @objc func handleLongGesture(gesture: UILongPressGestureRecognizer)
    {
        switch(gesture.state) {
        case .began:
            guard let selectedIndexPath = collectionview.indexPathForItem(at: gesture.location(in: collectionview)) else {
                break
            }
            collectionview.beginInteractiveMovementForItem(at: selectedIndexPath)
        case .changed:
            collectionview.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
        case .ended:
            collectionview.endInteractiveMovement()
        default:
            collectionview.cancelInteractiveMovement()
        }
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
}

extension ViewController:UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return str_arr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
    {
            return true
    }
    //This the ouptut part
    func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
    {
        let item = str_arr.remove(at: sourceIndexPath.item)
        str_arr.insert(item, at: destinationIndexPath.item)
        print(str_arr)
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DemoCollectionViewCell", for: indexPath) as! DemoCollectionViewCell
        cell.lbl_text.text = str_arr[indexPath.row]
        return cell
    }
    //Size
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: ((self.view.frame.size.width/5)), height: 200);
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
}

我遇到的问题是,当我第一次使用它时,这是屏幕: 我第一次启动 iPad 时的图像

当我拖动标签时,它们会变成正确的大小。这就是它应该从一开始的 样子:拖动标签后的样子

我不知道为什么会这样。我认为这与第 78 行有关,在那里我设置了标签的大小,但更改它似乎不起作用。拖动功能似乎工作正常,所以我认为第 27 到 42 行还可以。我可能是错的并且没有看到问题,但这就是我所做的。任何帮助都感激不尽。

标签: iosswiftxcodecollectionview

解决方案


我已经想通了。问题是情节提要的约束与 CGSize 宽度和高度相冲突,距离底部 5 行。因此,我将情节提要约束更改为我想要的大小,并通过在代码中将宽度更改为高度来为高度执行“self.view.frame.size.width/5”。就这样,一拍即合。其他你要考虑集合视图位置的标签,但从我的经验来看并不难处理。我希望这个答案可以帮助那些遇到这篇文章的人。


推荐阅读