单元格之间的间距不正确UICollectionView

时间:2017-11-23 13:26:12

标签: ios uicollectionview uicollectionviewcell

我有一个UICollectionView包含UICollectionViewCells,其中包含UIButton个内容。

但是每个单元格之间的间距不正确是不正确的。集合视图中的单元格大小不明确,因为它应该是一个标记列表,其长度明显不同。

看看它的样子:

enter image description here

每个单元格之间的水平间距完全不正确,因为它们在行之间不同。

单元格的代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    @IBOutlet weak var wordsCollection: UICollectionView!
    var items = ["test", "this", "word view", "like", "collection", "testing", "give", "this", "testing", "test", "test", "this", "word view", "like", "collection", "testing", "give", "this", "testing", "test", "test", "this", "word view", "like", "collection"]
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "wordCell", for: indexPath as IndexPath) as! WordCell
        cell.wordButton.setTitle(items[indexPath.row], for: UIControlState.normal)
        cell.wordButton.backgroundColor = UIColor.gray
        if(indexPath.row % 3 == 0){
            cell.wordButton.backgroundColor = UIColor.gray
        }
        cell.wordButton.clipsToBounds = true
        cell.wordButton.layer.cornerRadius = 2
        cell.wordButton.sizeToFit()

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if let flowLayout = wordsCollection.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSize(width:1, height:1) }
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

语言是Swift 4,我的目标是iOS 10.3。

1 个答案:

答案 0 :(得分:1)

实施:df1,df2 = [i.loc[:,i.columns[::-1]] for i in [df1,df2]] print(df1) b a 1 2 1 print(df2) c 1 4

然后:

UICollectionViewDelegateFlowLayout