gpt4 book ai didi

ios - 了解 UITableViewCell 子类中 UIButton 的逻辑

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

我的应用程序中有几个 UITableView 实例,它们都包含 UITableViewCell 子类的实例,称为 BeerTableViewCell。在我的子类中,我添加了几个 UILabel 实例和几个 UIButton 实例。 BeerTableViewCell 的代码如下:

import UIKit
import CoreData




class BeerTableViewCell: UITableViewCell {




//////////////////////////////////////////////////////////////
// MARK: - Properties
//////////////////////////////////////////////////////////////

var beerNameLabel = UILabel()
var breweryNameLabel = UILabel()
var beerCategoryReferenceImageView = UIImageView()
var likeButton = UIButton()
var tryButton = UIButton()
var dislikeButton = UIButton()
var deleteButton = UIButton()
var categoryButtonsArray = [UIButton]()


//////////////////////////////////////////////////////////////
// MARK: - View Lifecycle
//////////////////////////////////////////////////////////////

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.autoresizingMask = .FlexibleHeight
self.clipsToBounds = true
}




override func layoutSubviews() {
super.layoutSubviews()
beerNameLabel.frame = CGRectMake(10, 8, self.bounds.size.width - 40, 26)
beerNameLabel.font = UIFont(name: "Avenir Next", size: 18)
contentView.addSubview(beerNameLabel)

breweryNameLabel.frame = CGRectMake(10, 32, self.bounds.size.width - 40, 20)
breweryNameLabel.font = UIFont(name: "Avenir Next", size: 14)
breweryNameLabel.alpha = 0.5
contentView.addSubview(breweryNameLabel)

beerCategoryReferenceImageView.frame = CGRectMake(self.bounds.size.width-35, 20, 20, 20)
beerCategoryReferenceImageView.image = UIImage()
contentView.addSubview(beerCategoryReferenceImageView)

likeButton.frame = CGRectMake(20, 70, 30, 30)
contentView.addSubview(likeButton)

tryButton.frame = CGRectMake(90, 70, 30, 30)
contentView.addSubview(tryButton)

dislikeButton.frame = CGRectMake(160, 70, 30, 30)
contentView.addSubview(dislikeButton)

deleteButton.frame = CGRectMake(230, 70, 30, 30)
contentView.addSubview(deleteButton)

let likeNormalImage = UIImage(named: "like-button")
let likeSelectedImage = UIImage(named: "like-button")
likeButton.setImage(likeNormalImage, forState: .Normal)
likeButton.setImage(likeSelectedImage, forState: .Selected)

let tryNormalImage = UIImage(named: "try-button")
let trySelectedImage = UIImage(named: "try-button")
tryButton.setImage(tryNormalImage, forState: .Normal)
tryButton.setImage(trySelectedImage, forState: .Selected)

let dislikeNormalImage = UIImage(named: "dislike-button")
let dislikeSelectedImage = UIImage(named: "dislike-button")
dislikeButton.setImage(dislikeNormalImage, forState: .Normal)
dislikeButton.setImage(dislikeSelectedImage, forState: .Selected)

let deleteNormalImage = UIImage(named: "delete-button")
let deleteSelectedImage = UIImage(named: "delete-button")
deleteButton.setImage(deleteNormalImage, forState: .Normal)
deleteButton.setImage(deleteSelectedImage, forState: .Selected)1

likeButton.addTarget(self, action: "handleButtonPress", forControlEvents: .TouchUpInside)
tryButton.addTarget(self, action: "handleButtonPress", forControlEvents: .TouchUpInside)
dislikeButton.addTarget(self, action: "handleButtonPress", forControlEvents: .TouchUpInside)
deleteButton.addTarget(self, action: "handleButtonPress", forControlEvents: .TouchUpInside)

categoryButtonsArray.append(likeButton)
categoryButtonsArray.append(tryButton)
categoryButtonsArray.append(dislikeButton)
}




//////////////////////////////////////////////////////////////
// MARK: - States
//////////////////////////////////////////////////////////////

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}



//////////////////////////////////////////////////////////////
// MARK: - Helpers
//////////////////////////////////////////////////////////////

func handleButtonPress() {
println("Button Pressed")
}


}

在我的任何 UITableView 实例中按下按钮时,我很难找到执行操作的方法。我能够成功地向按钮添加一个 Action ,该 Action 将打印出按下了一个按钮 (println("Button Tapped"):

我无法弄清楚如何实现委托(delegate)方法来处理按钮按下。我发现的每个教程都通过 Interface Builder 实现了 UIButton 操作。我是否需要使用 Interface Builder 来创建按钮并为它们 Hook 操作?如您所见,我目前正在用代码创建我的按钮,但我发现的教程大部分都是这样做的 this方式。

不胜感激!

已解决:

为了让符合委托(delegate)协议(protocol)的 View Controller 允许将 self 分配给单元格的委托(delegate),我只需要在 View Controller 中实现委托(delegate)方法。如果我不添加委托(delegate)方法,Xcode 将不允许分配。

最佳答案

您有几个选择。您可以在 cellForRowAtIndexPath: 中调用 addTarget:action:forControlEvents: 而不是在您的单元类中(将自身作为目标传递),或者您可以使用委托(delegate)协议(protocol)(其中在这种情况下,您可以将自己设置为 cellForRowAtIndexPath 中单元格的委托(delegate))。在单元格类中创建委托(delegate)协议(protocol),添加委托(delegate)属性,并从按钮的操作方法中调用委托(delegate)方法。您将在 TableView Controller 中实现委托(delegate)方法以适本地响应按钮触摸。

关于ios - 了解 UITableViewCell 子类中 UIButton 的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995325/

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