Ich habe diese newCell Klasse:Wie kann ich andere Klasse Taste aufrufen - Swift
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) {
}
Im HomeViewController, verwende ich diese newCell Klasse als Prototyp Zelle für meine Tabellenansicht. Es funktioniert gut für Texte und Bilder. Mein Zweifel ist, wie kann ich einen Knopf dieser Prototypzelle in meinem HomeViewController handhaben? Zum Beispiel möchte ich, dass die Schaltfläche das Bild entsprechend dem übergebenen Array ändert.
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
}
Dieser Code macht keinen Sinn: 'myCell.favoriteAction (Absender: AnyObject) {' Die Deklaration für 'favoriteAction' ist bereits in der' newCell' Klasse. Das ist, wo der Code für das geht, was geschehen soll, wenn der Benutzer auf die Schaltfläche Favoriten klickt. – matt
Ok, tut mir leid, das ist ziemlich neu für mich. Wie kann der Code in newCell das Bild dieser Schaltfläche entsprechend einem Array von HomeViewController ändern? Ist das möglich? Danke im Voraus. –
Können Sie den Code für den HomeViewController posten? (oder zumindest einen Teil davon). @ RafaelDontalGonçalez – Ike10