gpt4 book ai didi

ios - 从 'AUAudioUnitBus?' 转换为无关类型 'UICollectionViewLayoutAttribute' 总是失败 - swift 2 iOS 9

转载 作者:行者123 更新时间:2023-11-29 01:31:07 25 4
gpt4 key购买 nike

我正在尝试修复另一个错误

Ambiguous use of 'subscript'

我得到模糊下标错误的旧代码是这样的:

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> (UICollectionViewLayoutAttributes!) {
return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
}

我得到不相关类型转换的新代码是这样的:

Error message

  override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> (UICollectionViewLayoutAttributes!) {
if let result: UICollectionViewLayoutAttributes = self.itemAttributes?[indexPath.section][indexPath.row] as? UICollectionViewLayoutAttributes {
return result
}else {
return nil
}
//return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
}

这是完整的类代码:

import UIKit

class CustomCollectionViewLayout: UICollectionViewLayout {

var numberOfColumns = 7 // the number of columns
var itemAttributes : NSMutableArray!
var itemsSize : NSMutableArray!
var contentSize : CGSize!

func setColumnNumber(columnNum: Int) {
numberOfColumns = columnNum
}

override func prepareLayout() {
if self.collectionView?.numberOfSections() == 0 {
return
}

if (self.itemAttributes != nil && self.itemAttributes.count > 0) {
for section in 0..<self.collectionView!.numberOfSections() {
let numberOfItems : Int = self.collectionView!.numberOfItemsInSection(section)
for index in 0..<numberOfItems {
if section != 0 && index != 0 {
continue
}

let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: index, inSection: section))
if section == 0 {
var frame = attributes.frame
frame.origin.y = self.collectionView!.contentOffset.y
attributes.frame = frame
}

if index == 0 {
var frame = attributes.frame
frame.origin.x = self.collectionView!.contentOffset.x
attributes.frame = frame
}
}
}
return
}

if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) {
self.calculateItemsSize()
}

var column = 0
var xOffset : CGFloat = 0
var yOffset : CGFloat = 0
var contentWidth : CGFloat = 0
var contentHeight : CGFloat = 0

for section in 0..<self.collectionView!.numberOfSections() {
let sectionAttributes = NSMutableArray()

for index in 0..<numberOfColumns {
let itemSize = self.itemsSize[index].CGSizeValue()
let indexPath = NSIndexPath(forItem: index, inSection: section)
let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = CGRectIntegral(CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height))

if section == 0 && index == 0 {
attributes.zIndex = 1024;
} else if section == 0 || index == 0 {
attributes.zIndex = 1023
}

if section == 0 {
var frame = attributes.frame
frame.origin.y = self.collectionView!.contentOffset.y
attributes.frame = frame
}
if index == 0 {
var frame = attributes.frame
frame.origin.x = self.collectionView!.contentOffset.x
attributes.frame = frame
}

sectionAttributes.addObject(attributes)

xOffset += itemSize.width
column++

if column == numberOfColumns {
if xOffset > contentWidth {
contentWidth = xOffset
}

column = 0
xOffset = 0
yOffset += itemSize.height
}
}
if (self.itemAttributes == nil) {
self.itemAttributes = NSMutableArray(capacity: self.collectionView!.numberOfSections())
}
self.itemAttributes .addObject(sectionAttributes)
}

let attributes : UICollectionViewLayoutAttributes = self.itemAttributes.lastObject?.lastObject as! UICollectionViewLayoutAttributes
contentHeight = attributes.frame.origin.y + attributes.frame.size.height
if( contentWidth == 0 || contentHeight == 0){return;}
self.contentSize = CGSizeMake(contentWidth, contentHeight)
}

override func collectionViewContentSize() -> CGSize {
if( self.contentSize != nil){
return self.contentSize
}else {
return CGSizeMake(0, 0)
}
}

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> (UICollectionViewLayoutAttributes!) {
if let result: UICollectionViewLayoutAttributes = self.itemAttributes?[indexPath.section][indexPath.row] as? UICollectionViewLayoutAttributes {
return result
}else {
return nil
}
//return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
}

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributes = [UICollectionViewLayoutAttributes]()
if self.itemAttributes != nil {
for section in self.itemAttributes {

let filteredArray = section.filteredArrayUsingPredicate(

NSPredicate(block: { (evaluatedObject, bindings) -> Bool in
return CGRectIntersectsRect(rect, evaluatedObject.frame)
})
) as! [UICollectionViewLayoutAttributes]


attributes.appendContentsOf(filteredArray)

}
}

return attributes
}

override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}

func sizeForItemWithColumnIndex(columnIndex: Int) -> CGSize {
let text : String = "25.10.15"
// switch (columnIndex) {
// case 0:
// text = "Col 0"
// case 1:
// text = "Col 1"
// case 2:
// text = "Col 2"
// case 3:
// text = "Col 3"
// case 4:
// text = "Col 4"
// case 5:
// text = "Col 5"
// case 6:
// text = "Col 6"
// default:
// text = "Col 7"
// }

let size : CGSize = (text as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(17.0)])
let width : CGFloat = size.width + 25
return CGSizeMake(width, 30)
}

func calculateItemsSize() {
self.itemsSize = NSMutableArray(capacity: numberOfColumns)
for index in 0..<numberOfColumns {
self.itemsSize.addObject(NSValue(CGSize: self.sizeForItemWithColumnIndex(index)))
}
}
}

最佳答案

错误没有多大意义,但我建议使用 Swift 数据类型而不是“NS”数据类型。

尝试将您的 NSMutableArray! 声明替换为:

var itemAttributes = [[UICollectionViewLayoutAttributes]]()

这为您提供了一个可以使用 [m][n] 索引访问的数组。

例子:

var itemAttributes = [[UICollectionViewLayoutAttributes]]()
var attrList = [UICollectionViewLayoutAttributes]()
let oneAttr = UICollectionViewLayoutAttributes()
attrList.append(oneAttr)

itemAttributes.append(attrList)

let attr = itemAttributes[0][0]

关于ios - 从 'AUAudioUnitBus?' 转换为无关类型 'UICollectionViewLayoutAttribute' 总是失败 - swift 2 iOS 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443305/

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