Jay*_*bey 5 xcode ios uicollectionview uicollectionviewcell swift
我的自定义UICollectionViewCell包含UILabel和UIImage.我想以圆形格式设置单元格.
注意: - sizeForItemAtIndexPath由于AutoLayout而无法更改.
以下是我用过的代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath ) -> CGSize {
return CGSize(width: (self.objKeypadCollectionView.frame.size.width/3)-10, height: (self.objKeypadCollectionView.frame.size.height/4)-10)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let objKeypadCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("idKeypadCollectionViewCell", forIndexPath: indexPath) as! KeypadCollectionViewCell
if (self.numberArray[indexPath.item])=="asda" {
objKeypadCollectionViewCell.lblNumber.text = ""
objKeypadCollectionViewCell.imgPrint.hidden = false
}
else if (self.numberArray[indexPath.item])=="Derterel" {
objKeypadCollectionViewCell.lblNumber.text = self.numberArray[indexPath.item]
objKeypadCollectionViewCell.imgPrint.hidden = true
}
else {
objKeypadCollectionViewCell.imgPrint.hidden = true
objKeypadCollectionViewCell.lblNumber.text = self.numberArray[indexPath.item]
objKeypadCollectionViewCell.layer.borderColor = UIColor.lightGrayColor().CGColor
objKeypadCollectionViewCell.layer.borderWidth = 1
objKeypadCollectionViewCell.layer.cornerRadius = objKeypadCollectionViewCell.frame.size.width/2
}
return objKeypadCollectionViewCell
}
Run Code Online (Sandbox Code Playgroud)
onC*_*ion 11
这是我的UICollectionView示例,您可以检查您的解决方案.我正在使用storyboard来设置带有AutoLayout的主集合.我也为这些东西附上了一些截图.
有一种叫做的方法drawRect.您可以在collectionView Cell类中使用它来执行UI内容.
现在这是我的代码.
1. ViewController.swift //就像普通的ViewController一样,没有别的...... :)
//
// ViewController.swift
// CollectionCheckCircle
//
// Created by Tuhin Samui on 02/09/16.
// Copyright © 2016 Tuhin Samui. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 200
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cellOfCollection = collectionView.dequeueReusableCellWithReuseIdentifier("cellCollectionView", forIndexPath: indexPath) as! CollectionViewCell
return cellOfCollection
}
}
Run Code Online (Sandbox Code Playgroud)
2. CollectionViewCell.swift //只是一个用于出列单元格的UICollectionViewCell类.
//
// CollectionViewCell.swift
// CollectionCheckCircle
//
// Created by Tuhin Samui on 02/09/16.
// Copyright © 2016 Tuhin Samui. All rights reserved.
//
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
override func drawRect(rect: CGRect) { //Your code should go here.
super.drawRect(rect)
self.layer.cornerRadius = self.frame.size.width / 2
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我没有UICollectionViewFlowLayout在sizeForItemAtIndexPath此collectionView中使用任何flowlayout 或cell size .你也可以添加这些东西.请根据您的需要做一些实验.顺便说一下,我使用的是Xcode 7.3.1和Swift 2.2.
现在是时候在模拟器上设置collectionView设置和结果的故事板截图.这部分必须在编码部分之前完成....:p
希望这有帮助.对不起,对不起.