未选择自定义CollectionView单元,但在单元空间之外注册选择

时间:2018-07-03 04:59:29

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewdelegate

view example

在上面的示例中,单击蓝色单元格不会触发任何collectionView委托方法,例如“ didSelectItem”。但是,直接单击剩余的空白即可。它是一个自定义单元格,实际上只有一个文本视图位于带有蓝色背景的UIView顶部。这是预期的行为吗?

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCell

    let message = messages[indexPath.item]
    cell.textView.text = message.text

    setupCell(cell: cell, message: message)

    if let text = message.text {
     cell.bubbleWidthAnchor?.constant = estimateFrameForText(text: text).width + 32
    } else if message.imageUrl != nil {
        cell.bubbleWidthAnchor?.constant = 200
    }

    return cell
}

private func setupCell(cell: ChatMessageCell, message: Message) {
    if let profileImageUrl = self.user?.profileImageUrl {
        cell.profileImageView.loadImageUsingCacheWithUrlString(urlString: profileImageUrl)
    }

    if message.fromId == Auth.auth().currentUser?.uid {
        cell.bubbleView.backgroundColor = ChatMessageCell.blueColor
        cell.textView.isHidden = false
        cell.textView.textColor = UIColor.white
        cell.profileImageView.isHidden = true
        cell.messageImageView.isHidden = true

        cell.bubbleViewRightAnchor?.isActive = true
        cell.bubbleViewLeftAnchor?.isActive = false
    } else {
        cell.bubbleView.backgroundColor = UIColor(r: 220, g: 220, b: 220)
        cell.textView.textColor = UIColor.black
        cell.profileImageView.isHidden = false
        cell.messageImageView.isHidden = true

        cell.bubbleViewRightAnchor?.isActive = false
        cell.bubbleViewLeftAnchor?.isActive = true
    }

    if let messageImageUrl = message.imageUrl {
        cell.messageImageView.loadImageUsingCacheWithUrlString(urlString: messageImageUrl)
        cell.textView.isHidden = true
        cell.messageImageView.isHidden = false
        cell.bubbleView.backgroundColor = UIColor.clear
    } else {
        cell.messageImageView.isHidden = true
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    var height: CGFloat = 80

    let message = messages[indexPath.item]

    if let text = message.text {
        height = estimateFrameForText(text: text).height + 18
    } else if let imageWidth = message.imageWidth?.floatValue, let imageHeight = message.imageHeight?.floatValue {

        height = CGFloat(imageHeight / imageWidth * 200)

    }

    let width = UIScreen.main.bounds.width
    return CGSize(width: width, height: height)
}

private func estimateFrameForText(text: String) -> CGRect {
    let size = CGSize(width: 200, height: 1000)
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) // look this up on stack overflow

    return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], context: nil)
}

0 个答案:

没有答案