我有一个自定义单元格类,如下所示:
class CollectionViewCell: UICollectionViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var textLabel: UILabel = UILabel()
var buttonView: UIButton!
var id: String = String()
override init(frame: CGRect) {
super.init(frame: frame)
let textFrame = CGRect(x: buttonView.center.x - self.frame.width/2, y: 90, width: frame.size.width, height: frame.size.height/3)
textLabel = UILabel(frame: textFrame)
textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
textLabel.textAlignment = .Center
contentView.addSubview(textLabel)
buttonView = UIButton.buttonWithType(UIButtonType.System) as! UIButton
buttonView.frame = CGRect(x: frame.width / 2 - frame.width * 0.4, y: 0 + textLabel.frame.size.height, width: frame.size.width * 0.8, height: frame.size.height * 0.8 )
contentView.addSubview(buttonView)
}
}
和我出列队列的函数看起来像这样:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
switch collectionView{
case collectionViewOne!:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewDetailCell", forIndexPath: indexPath) as! CollectionViewDetailCell
cell.backgroundColor = UIColor.whiteColor()
cellHeightOne = cell.frame.height
cell.productLabel.text = "\(array[indexPath.row].name)"
cell.textLabel.numberOfLines = 0
let myText = NSString(format: self.array[indexPath.row].description)
let paragraphStyle = NSMutableParagraphStyle() as NSMutableParagraphStyle
paragraphStyle.alignment = NSTextAlignment.Justified
let attributes: NSDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSBaselineOffsetAttributeName: 0, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName: UIColor.grayColor()]
let attributedText = NSAttributedString(string: myText as String, attributes: attributes as [NSObject : AnyObject])
cell.textLabel.attributedText = attributedText
cell.textLabel.sizeToFit()
cell.contentView.addSubview(cell.textLabel)
return cell
我希望我的标签文字高度按下按钮,但没有任何反应,我也试图找出高度,并将其发送到单元格,如:
cell.textHeigth = textHeigth,并使用textHeigth
buttonView.frame = CGRect(x: frame.width / 2 - frame.width * 0.4, y: 0 + textHeigth, width: frame.size.width , height: frame.size.height * 0.8)
但后来我得到一些变量为零的错误,但我尝试打印变量,然后将它发送到customClass之前它不是零
答案 0 :(得分:1)
实现以下委托方法以设置集合视图单元格的宽度和高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(width,height);
}
答案 1 :(得分:0)
我设法使用以下代码将我的按钮放在文本下面:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewDetailCell", forIndexPath: indexPath) as! CollectionViewDetailCell
let myText = NSString(format: newString)
let paragraphStyle = NSMutableParagraphStyle() as NSMutableParagraphStyle
paragraphStyle.alignment = NSTextAlignment.Justified
let attributes: NSDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSBaselineOffsetAttributeName: 0, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName: UIColor.grayColor()]
let attributedText = NSAttributedString(string: myText as String, attributes: attributes as [NSObject : AnyObject])
cell.textLabel.attributedText = attributedText
cell.textLabel.sizeToFit()
cell.contentView.addSubview(cell.textLabel)
cell.buttonView.frame = CGRect(x: 0, y: 200 + cell.textLabel.frame.size.height, width: 10, height: 10)
return cell
}
你必须在这个cellForItemAtIndexPath
中做所有事情