gpt4 book ai didi

ios - Swift 中的 UICollectionView 部分边框

转载 作者:行者123 更新时间:2023-11-28 06:42:46 25 4
gpt4 key购买 nike

我正在尝试使用 UICollectionView 将不同的单元格与 XIB 文件绑定(bind)并为其设置设计。

我知道如何绑定(bind)不同的单元格,并且它在我的应用程序中运行良好。

这里是绑定(bind)代码:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

var cell : UICollectionViewCell
mCurrentIndexPath = indexPath

// HEADER
switch indexPath.section {
case 0:
cell = configureModuleHeaderCell(indexPath)
default:
// Local
let theme = getThemeFromIndex(indexPath.section - 1)
mCurrentDocuments = getDocumentsFromTheme(theme)
let cours : DownloadableDocument? = (mCurrentDocuments != nil) ? getCoursForTheme() : nil
mCurrentDocuments = deleteCoursFromDocAnnexe()
mCurrentDocuments = sortDocumentDoublePDF()
if indexPath.row == 0 {
cell = configureThemeHeaderCell(theme, cours: cours)
}

// NORMAL
else {
cell = configureThemeDocCell()
cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.grayColor().CGColor
}
break
}

return cell
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
if mFetchedResultController != nil {
mCurrentIndexPath = NSIndexPath(forRow: 0, inSection: 0)
return mFetchedResultController!.fetchedObjects!.count + 1
}
return 0
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var counter : Int = 1
switch section {
case 0:
counter = 1
break
default:
mCurrentDocuments = getDocumentsFromTheme(getThemeFromIndex(section - 1))
mCurrentDocuments = checkDoubleCours()
counter = (mCurrentDocuments != nil) ? mCurrentDocuments!.count : 0
counter += (!documentsContainsCours(mCurrentDocuments)) ? 1 : 0
break
}
return counter
}

然后我想为每个 section 设置一个 border。这可能吗?

我可以通过以下方式为单元格设置边框:

cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.grayColor().CGColor

但我想为 部分 做这件事。

最佳答案

我终于找到了解决办法!

我只需要计算一个部分的所有单元格的高度,在 View 上设置这个高度并将其定位好。

编辑:@Urmi 的更新

添加此函数以实例化带框架的 cardView

func instanciateCardView(frame : CGRect) -> UIView {
// Declaration
let view = UIView(frame : frame)

// Basic UI
view.backgroundColor = UIColor.whiteColor()
view.layer.backgroundColor = UIColor.whiteColor().CGColor
view.layer.borderWidth = 1.0
view.layer.borderColor = ColorsUtil.UIColorFromRGB(0xAB9595).CGColor

// Shadow (cardview style)
view.layer.shadowPath = UIBezierPath(rect: view.bounds).CGPath
view.layer.shouldRasterize = true
view.layer.shadowColor = ColorsUtil.UIColorFromRGB(0xD5C6C6).CGColor
view.layer.shadowOpacity = 1
view.layer.shadowOffset = CGSizeMake(1, 1);
view.layer.shadowRadius = 1.0

if !mCardViewList.contains(view) {
// Add view to container - Just to save the instance
mCardViewList.append(view)

// Add view to main view
self.collectionView?.addSubview(view)
self.collectionView?.sendSubviewToBack(view)
}

return view
}

cellForItemAtIndexPath 中:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell : UICollectionViewCell
mCurrentIndexPath = indexPath
switch indexPath.section {
case 0:
cell = configureModuleHeaderCell(indexPath)

// Basic UI
cell.backgroundColor = UIColor.whiteColor()
cell.layer.borderWidth = 1.0
cell.layer.borderColor = ColorsUtil.UIColorFromRGB(0xAB9595).CGColor

// Shadow (cardview style)
cell.layer.shadowPath = UIBezierPath(rect: view.bounds).CGPath
cell.layer.shouldRasterize = true
cell.layer.shadowColor = ColorsUtil.UIColorFromRGB(0xD5C6C6).CGColor
cell.layer.shadowOpacity = 1
cell.layer.shadowOffset = CGSizeMake(1, 1);
cell.layer.shadowRadius = 1.0

break
default:
let theme = getThemeFromIndex(indexPath.section - 1)
mCurrentDocuments = getDocumentsFromTheme(theme)
let cours : DownloadableDocument? = (mCurrentDocuments != nil) ? getCoursForTheme() : nil
mCurrentDocuments = deleteCoursFromDocAnnexe()
mCurrentDocuments = sortDocumentDoublePDF(mCurrentDocuments)

if indexPath.row == 0 {
cell = configureThemeHeaderCell(theme, cours: cours)
if mCurrentDocuments != nil {
if mCurrentDocuments!.count == 0 {
instanciateCardView(CGRectMake(cell.frame.origin.x - 30, cell.frame.origin.y - 10, cell.frame.width + 60, cell.frame.height + 20))
}
}
}
else {
cell = configureThemeDocCell()
if indexPath.row == mCollectionView.numberOfItemsInSection(indexPath.section) - 1 {

// Attribute
let index = NSIndexPath(forRow: 0, inSection: indexPath.section)
let headerAttribute : UICollectionViewLayoutAttributes = mCollectionView.layoutAttributesForItemAtIndexPath(index)!
let height = (cell.frame.origin.y + cell.frame.height) - (headerAttribute.frame.origin.y - 10)

instanciateCardView(CGRectMake(headerAttribute.frame.origin.x - 30, headerAttribute.frame.origin.y - 10, headerAttribute.frame.width + 60, height + 20))
}
}
break
}

return cell
}

我发现这不是最好的解决方案,但它确实有效。现在我已经声明了一个 UITableView 并在每个 UITableViewCell

中添加了一个 UICollectionView

如果对你有帮助,别忘了点个赞:)

关于ios - Swift 中的 UICollectionView 部分边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37478971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com