单击UICollectionViewCell时更改按钮背景图像

时间:2016-10-25 06:04:19

标签: ios uibutton uicollectionview swift3 uicollectionviewcell

我有一个collectionView,集合视图的每个单元格都有多个按钮。我想更改我在特定单元格中单击的按钮的背景图像,就像它在facebook应用程序中的类似按钮的工作原理一样。

enter image description here

我创建了一个自定义Feed类并将其链接到cellForitemAt方法:

let cell: FeedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! FeedCell

return cell

饲料细胞:

import Foundation
import UIKit

class FeedCell: UICollectionViewCell
{


    @IBOutlet weak var userNameText: UILabel!

    @IBOutlet weak var userProfilePic: UIImageView!

    @IBOutlet weak var foodPic: UIImageView!

    @IBOutlet weak var likePic: UIImageView!

    @IBOutlet weak var likeText: UILabel!

    @IBOutlet weak var dislikePic: UIImageView!

    @IBOutlet weak var dislikeText: UILabel!

    @IBOutlet weak var favPic: UIImageView!

    @IBOutlet weak var favText: UILabel!

    @IBOutlet weak var commentPic: UIImageView!

    @IBOutlet weak var commentText: UILabel!

    @IBOutlet weak var sharePic: UIImageView!

    @IBOutlet weak var shareCount: UILabel!



 @IBAction func TempTest(_ sender: UIButton) {
    print("Pressed")
 }  
}

你能否帮助我实现同样的目标。

由于

1 个答案:

答案 0 :(得分:0)

请参阅以下代码。

您的UICollectionViewCell类

protocol FeedCellDelegate{
func didPress(with type:FeedCell.ClickedButtonType, senderImageView imageView:UIImageView, withcell cell:FeedCell)
}

class FeedCell: UICollectionViewCell
{
enum ClickedButtonType {
    case userProfilePic,foodPic,likePic,likeText,dislikePic,favPic,commentPic,sharePic
}

var delegate : FeedCellDelegate?


@IBOutlet weak var userNameText: UILabel!

@IBOutlet weak var userProfilePic: UIImageView!

@IBOutlet weak var foodPic: UIImageView!

@IBOutlet weak var likePic: UIImageView!

@IBOutlet weak var likeText: UILabel!

@IBOutlet weak var dislikePic: UIImageView!

@IBOutlet weak var dislikeText: UILabel!

@IBOutlet weak var favPic: UIImageView!

@IBOutlet weak var favText: UILabel!

@IBOutlet weak var commentPic: UIImageView!

@IBOutlet weak var commentText: UILabel!

@IBOutlet weak var sharePic: UIImageView!

@IBOutlet weak var shareCount: UILabel!


@IBAction func TempTest(_ sender: UIButton) {
    //Call the delegate method and pass the values according to your action just for example i have used commentPic state you can put a switch case or if-else
    delegate?.didPress(with: .commentPic, senderImageView: commentPic, withcell: self)
}
}

和您的视图控制器。

//Your controller implements FeedCellDelegate
class YourViewController : UIViewController,FeedCellDelegate,UICollectionViewDataSource{

@IBOutlet var collectionView : UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1//Use your array to return the count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: FeedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! FeedCell
    cell.delegate = self
    return cell
}

func didPress(with type:FeedCell.ClickedButtonType, senderImageView imageView:UIImageView, withcell cell:FeedCell){
    //Get the indexPath
    let indexPath = collectionView.indexPath(for: cell)
    //remove the break and put all other enum cases and you can handle the cases and change the image according to your logic
    switch type {
    case .userProfilePic: break
    default: break
    }
}
}

希望这有帮助。