简体   繁体   中英

CollectionView objects (Swift)

I want the highlight to change the size and appearance of an object inside the collection view.

How can I set object properties in a collection view cell, within the "didHighlight" method?

In "cellForItemAtIndexPath" you declare the reusable cells as the class

and just use "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 { } } 

I tried using a similar definition in didHighlight and it keeps crashing.

Let didHighlightItemAtIndexPath only change the data, not the view. So, make friends[indexPath.row] an object or add another parameter to tuple. And in didHighlightItemAtIndexPath do something like the following:

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

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

And in 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;
    }

} 

EDIT: Updated, so instead of objects it uses tuples. Also added the functionality that you need. Basically, you need to create two prototype cells in the interface builder with different Reuse Identifiers and Classes. And then dequeue the correct identifier in the index path. Also, I refactored some of your code and if I were you I would create a different function for each collectionView and do something like:

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

Also, the code that you provided... I hope it is not an actual production code and you changed the values for this forum. Otherwise, in couple of days even, you are going to get lost in what is happening here. Too many inconsistent variable names and identifiers.

One more also. Use naming conventions in your class names. Read this forum post for more information. Apple uses camelCase everywhere. In majority of instances, the first letter is capitalized for class names, not object names.

first you have to define the collectionView Cell then do what ever you want on that cell. to define your sell add the below lines into didHighlightItemAtIndexPath

if let cellToUpdate = self.dataCollection.cellForItemAtIndexPath(indexPath) {
                                //your code here.
                            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM