gpt4 book ai didi

ios - CollectionView 对象 (Swift)

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

我希望突出显示可以更改 Collection View 中对象的大小和外观。

如何在“didHighlight”方法中设置 Collection View 单元格中的对象属性?

在“cellForItemAtIndexPath”中,您将可重用单元格声明为类

只需使用“cell.MyOutlet.backgroundColor = UIColor.blueColor()”

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

if collectionView == self.CollectionViewController {
let (FriendFirstName,FriendLastName) = friends[indexPath.row]

let cell: CustomCellA = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell", forIndexPath: indexPath) as! CustomCellA

if indexPath.section == 0 {
cell.cellTitle.text = Name
cell.imgCell.image = UIImage(named: Pics[indexPath.row])

cell.imgCell.layer.masksToBounds = true
cell.self.imgCell.layer.cornerRadius = 20

return cell


} else {

let cell2: AddCell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell2", forIndexPath: indexPath) as! AddCell
return cell2
}
} else if collectionView == self.EmojiCollectionViewController {
let cellB: CustomCellB = collectionView.dequeueReusableCellWithReuseIdentifier("demoCellB", forIndexPath: indexPath) as! CustomCellB

cellB.MyLabel.text = arrayOne[indexPath.row]

return cellB

} else {
let cellC: CustomCellC = collectionView.dequeueReusableCellWithReuseIdentifier("demoCellC", forIndexPath: indexPath) as! CustomCellC

// ...Set up cell
let height = self.CollectionViewController2.frame.height

cellC.frame = CGRectMake(cellB.frame.origin.x, 0, cellB.frame.size.width, height)
cellC.updateConstraintsIfNeeded()
cellC.layoutIfNeeded()
cellC.imgVw.image = UIImage(named: pictures[indexPath.row] as! String)

return cellC
}

}

    
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {

if collectionView == self.CollectionViewController {

if indexPath.section == 0 {
let cell: CustomCellA = CustomCellB()
cell.MyLabel.backgroundColor = UIColor.blueColor() //crashes due to nil value)

}

} else {


}


}

我尝试在 didHighlight 中使用类似的定义,但它总是崩溃。

最佳答案

didHighlightItemAtIndexPath 只改变数据,不改变 View 。因此,使 friends[indexPath.row] 成为对象或向元组添加另一个参数。在 didHighlightItemAtIndexPath 中执行如下操作:

 if collectionView == self.CollectionViewController {
if indexPath.section == 0 {
let (fname, lname, color) = friends[indexPath.row];

friends[indexPath.row] = (fname, lname, UIColor.blueColor())
}
}

cellForItemAtIndexPath 中:

if collectionView == self.CollectionViewController {
let (FriendFirstName, FriendLastName, color) = friends[indexPath.row]

if indexPath.section != 0 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell2", forIndexPath: indexPath) as! AddCell;
return cell;
} else if color == nil {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCell", forIndexPath: indexPath) as! CustomCellA;

cell.cellTitle.text = Name
cell.imgCell.image = UIImage(named: Pics[indexPath.row])

cell.imgCell.layer.masksToBounds = true
cell.self.imgCell.layer.cornerRadius = 20

return cell
} else {
cell = collectionView.dequeueReusableCellWithReuseIdentifier("demoCellB", forIndexPath: indexPath) as! CustomCellB;

// your code for CustomCellB

return cell;
}

}

编辑:已更新,因此它使用元组代替对象。还添加了您需要的功能。基本上,您需要在界面构建器中使用不同的重用标识符和类创建两个原型(prototype)单元。然后在索引路径中出列正确的标识符。此外,我重构了您的一些代码,如果我是您,我会为每个 collectionView 创建一个不同的函数并执行如下操作:

if collectionView == self.CollectionViewController {
return self.dequeueCollectionCell(indexPath);
} else if collectionView == self.EmojiCollectionViewController {
return self.dequeuEmojiCell(indexPath);
} else {
return self.dequeueSomeOtherCell(indexPath);
}

此外,您提供的代码...我希望它不是实际的生产代码,并且您更改了该论坛的值。否则,甚至在几天之内,你就会迷失在这里发生的事情中。太多不一致的变量名和标识符。

又一也。在类名中使用命名约定。阅读此 forum post了解更多信息。 Apple 到处都使用驼峰命名法。在大多数情况下,第一个字母大写表示类名,而不是对象名。

关于ios - CollectionView 对象 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29858314/

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