【问题标题】:Changing button background image when clicked UICollectionViewCell单击 UICollectionViewCell 时更改按钮背景图像
【发布时间】:2017-03-07 01:27:31
【问题描述】:

我有一个collectionView,并且collection view 的每个单元格都有多个按钮。我想更改在特定单元格中单击的按钮的背景图像,就像它在 facebook 应用程序中的 like 按钮的工作方式一样。

我创建了一个自定义提要类并将其链接到 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")
 }  
}

你能帮我实现同样的目标吗?

谢谢

【问题讨论】:

标签: ios uibutton uicollectionview swift3 uicollectionviewcell


【解决方案1】:

请参考以下代码。

你的 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
    }
}
}

希望这会有所帮助。

【讨论】:

  • 我在 cell.delegate = self "类型的值 'FeedCell' 没有成员 'delegate' 处收到错误。
  • 查看 var delegate : FeedCellDelegate?在我声明的枚举下方,您是否将其放入您的代码中?
  • 我忘记了。我在其中一个 switch 案例中编写了一个打印语句,但我没有得到任何输出。
  • 你的 TempTest 被调用了吗?
猜你喜欢
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多