我有这个 newCell 类:
class newCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBOutlet weak var recipeImage: UIImageView!
@IBOutlet weak var recipeTitle: UILabel!
@IBOutlet weak var favoriteButton: UIButton!
@IBAction func favoriteAction(sender: AnyObject) {
}
在 HomeViewController 中,我使用这个 newCell 类作为我的 TableView 的原型(prototype)单元格。它适用于文本和图像。我的疑问是,如何在我的 HomeViewController 中处理这个原型(prototype)单元格的按钮? 例如,我希望按钮根据传递的数组更改其图像。
override func viewDidLoad() {
super.viewDidLoad()
var recipesFeed = PFQuery(className: "Recipe")
recipesFeed.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let recipes = objects {
self.recipes.removeAll(keepCapacity: true)
self.recipeImages.removeAll(keepCapacity: true)
self.isFavorite.removeAll(keepCapacity: true)
for recipe in recipes{
self.recipes.append(recipe["title"] as! String)
self.recipeImages.append(recipe["image"] as! String)
self.isFavorite.append(recipe["isFavorite"] as! Int)
self.tableView.reloadData()
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = self.tableView.dequeueReusableCellWithIdentifier("prototypeCell") as! newCell
images[indexPath.row].getDataInBackgroundWithBlock { (image, error) -> Void in
if let downloadedImage = UIImage(data: image!){
myCell.recipeImage.image = downloadedImage
} else {
myCell.recipeImage.image = UIImage(named: "background_recipe_image.png")
}
}
myCell.recipeTitle.text = recipes[indexPath.row]
//begin what I try and doesn't work
myCell.favoriteAction(sender: AnyObject) {
if isFavorite[indexPath.row] == 1 {
if let image = UIImage(named: "star vector.png") {
myCell.favoriteButton.setImage(image, forState: .Normal)
}
} else {
if let image = UIImage(named: "star vector blank.png") {
myCell.favoriteButton.setImage(image, forState: .Normal)
}
}
}
//end what I try and doesn't work
return myCell
}
最佳答案
因此,我访问 HomeViewController 中的 isFavorite
数组的方法是使用具有两种协议(protocol)的委托(delegate)模式。
protocol hasFavBtn {
var delegate: FavBtnDelegate! { get set }
var delegateTableView: UITableView! { get set }
}
protocol FavBtnDelegate {
var isFavorited: [Int] { get set }
}
为了使其正常工作,NewCell
类应符合 hasFavBtn
协议(protocol),而 HomeViewController 应符合 FavBtnDelegate
协议(protocol)。像这样:
主视图 Controller
class ViewController: UITableViewController, FavBtnDelegate {
var isFavorited = [0, 0, 0, 0, 1]
let newCellStr = "NewCell"
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let newCell = tableView.dequeueReusableCellWithIdentifier(newCellStr) as! NewCell
newCell.delegate = self
newCell.delegateTableView = tableView
return newCell
}
}
这里需要注意的重要事项是:
在
cellForRowAtIndexPath
中,定义了NewCell
类实例的delegate
和delegateTableView
属性在返回newCell
常量之前。isFavorited
变量按照FavBtnDelegate
协议(protocol)中的描述进行设置。
NewCell.swift
class NewCell: UITableViewCell, hasFavBtn {
var delegate: FavBtnDelegate!
var delegateTableView: UITableView!
@IBOutlet weak var favoriteBtn: UIButton!
let blankStarImg = UIImage(imageLiteral: "blankStar")
let fullStarImg = UIImage(imageLiteral: "fullStar")
override func awakeFromNib() {
super.awakeFromNib()
let blankStarImg = UIImage(imageLiteral: "blankStar")
favoriteBtn.setBackgroundImage(blankStarImg, forState: .Normal)
}
@IBAction func favoriteBtnPressed(sender: AnyObject) {
let locationInCell = CGPoint(x: (sender.frame.maxX - sender.frame.minX)/2, y: (sender.frame.maxY - sender.frame.minY)/2)
let locationInDelegateTableView = convertPoint(locationInCell, toView: delegateTableView)
if let indexPath = delegateTableView.indexPathForRowAtPoint(locationInDelegateTableView) {
if delegate.isFavorited[indexPath.row] == 1 {
favoriteBtn.setBackgroundImage(fullStarImg, forState: .Normal)
}
}
}
正如其他人所说,在 UITableViewCell
子类中,您应该为最喜欢的 Button 定义 @IBAction
。您可以通过委托(delegate)属性引用 tableView 和 isFavorited
数组,因为在 cellForRowAtIndexPath
中,delegateTableView
属性的值被设置为HomeViewController 和 delegate
属性设置为 HomeViewController 实例(使用 self
关键字)。
所以 favoriteBtnPressed
函数找到被按下按钮的中心(CGPoint
),然后将其转换为 delegateTableView
的坐标系以及 HomeViewController 中的 tableView。在 tableView 的坐标系中使用按钮的转换中心,代码使用 if let 绑定(bind)安全地解包转换中心的 indexPath
。然后使用展开的 indexPath
和 delegate
属性,您可以检查 HomeViewController 的 isFavorited
数组值是否位于给定的 indexPath
等于 1 并相应地调整图像。
swift 的协议(protocol)和委托(delegate)模式非常强大,对很多事情都很有用。您可以在这里阅读更多关于它们的信息:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
另请注意,我将图像放在 Assets.xcassets 文件中,因此我使用 imageLiteral 初始化程序来获取它们。
希望对您有所帮助!如果您有任何问题,请随时提出!
关于ios - 我怎样才能调用其他类按钮 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37765677/