如何使collectionview单元格在swift中靠近另一个单元格 [英] How to make collectionview cell come close to another cell in swift

查看:50
本文介绍了如何使collectionview单元格在swift中靠近另一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableview 单元格中使用 collectionview 并根据

根据下面的答案并删除 awakeFromNib 中的代码,然后是 o/p

我错在哪里

解决方案

需要设置 insets

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) ->UIEdgeInsets {让 cellWidth = 50让 noOfCellsInRow = 2让 flowLayout = collectionViewLayout 作为!UICollectionViewFlowLayout让空间 = flowLayout.minimumInteritemSpacing让 totalCellWidth = cellWidth * noOfCellsInRow让 leftInset = (collectionView.frame.width - (CGFloat(totalCellWidth) + space))/2让 rightInset = leftInset返回 UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)}

I am using collectionview in tableview cell and showing collectionview height according to number of cells from JSON response according to this answer

the code:

class ViewProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {


@IBOutlet weak var attCollHeight: NSLayoutConstraint!
@IBOutlet weak var attetchmentsCollectionview: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    let width = UIScreen.main.bounds.width/2.5
    let height = CGFloat(40)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.itemSize = CGSize(width: width, height: height)
    self.attetchmentsCollectionview.collectionViewLayout = layout
}
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "ViewProposalTableVIewCell", for: indexPath) as! ViewProposalTableVIewCell

let bidData = viewproposalData?.result?.bids?[indexPath.row]
cell.propAmt.text = "$\(bidData?.amount ?? "")"

cell.frame = tableView.bounds
cell.layoutIfNeeded()
cell.attetchmentsCollectionview.reloadData()

cell.attCollHeight.constant = cell.attetchmentsCollectionview.collectionViewLayout.collectionViewContentSize.height;

cell.attetchmentsCollectionview.reloadData()
return cell
}

then the 0/p: but i don't want the gap between the cells how to make both cells come close

EDIT: according to below answer and removed code in awakeFromNib then the o/p

where am i wrong

解决方案

You need to set insets

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    let cellWidth = 50
    let noOfCellsInRow = 2
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    let space = flowLayout.minimumInteritemSpacing
    let totalCellWidth = cellWidth * noOfCellsInRow
    
    let leftInset = (collectionView.frame.width - (CGFloat(totalCellWidth) + space)) / 2
    let rightInset = leftInset

    return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}

这篇关于如何使collectionview单元格在swift中靠近另一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆