ios - 如何在 CollectionViewCell 中设置元素的框架?

标签 ios swift uicollectionview

我有一个看起来像这样的自定义单元类:

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) 但后来我得到一些变量为 nil 的错误,但我尝试打印该变量并且在将它发送到 customClass 之前它不是 nil

最佳答案

实现下面的委托(delegate)方法来设置 Collection View 单元格的宽度和高度

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
     return CGSizeMake(width,height);
 }

关于ios - 如何在 CollectionViewCell 中设置元素的框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663063/

相关文章:

swift - 为什么红蜘蛛不再适用于真正的 Apple Watch (WatchOS 6)?

ios - 当具有动态单元格高度的 UITableview 放置在里面时,如何扩展/缩小 UICollectionViewCell 高度?

ios - UICollectionView - iOS 10 - 在 iPhone 6 Plus 模拟器上崩溃,但在真实设备上运行

ios - 这些日光/夜晚长度近似值的错误在哪里?

ios - 使用 UIViewPropertyAnimator 通过触摸中断动画

iphone - 当条件变为真时禁用后退按钮对 ios 来说是一种良好的用户体验吗?

ios - 当一个应用程序打开时,如何让用户输入的两个警报一个接一个打开?

ios - id 标识符 swift

ios - 自动布局 : What creates constraints named UIView-Encapsulated-Layout-Width & Height?

ios - 使用 NSJSONSerialization 反序列化压缩的 JSON 文件