我的代表在协议中的使用存在问题,很多人面临同样的问题,但没有答案适合我。
我的第一堂课是FavorisHeaderTableView
import UIKit
protocol FavorisHeaderDelegate {
func changeFavoris(sender: FavorisHeaderTableViewCell)
}
class FavorisHeaderTableViewCell: UITableViewCell {
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var txtFavoriteNameInput: UITextField!
@IBOutlet weak var lblIcon: UILabel!
@IBOutlet weak var lblTime: UILabel!
@IBOutlet weak var lblDescription: UILabel!
@IBOutlet weak var btnFavHeart: UIButton!
@IBOutlet weak var btnFavHome: UIButton!
@IBOutlet weak var btnFavShop: UIButton!
@IBOutlet weak var btnFavWork: UIButton!
@IBOutlet weak var btnFavGolf: UIButton!
var myDelegate: FavorisHeaderDelegate? = nil
var defaultIcon:FavIconType = .heart
var selectedIcon:UIButton? = nil {
didSet {
selectedIcon!.backgroundColor = Design.Palette.red
selectedIcon?.layer.cornerRadius = 3
selectedIcon?.tintColor = Design.Palette.white
}
willSet {
if selectedIcon != nil {
selectedIcon!.backgroundColor = UIColor.clear
selectedIcon?.tintColor = UIColor(red:0.671, green:0.651, blue:0.635, alpha:1)
}
}
}
@IBAction func didSelectIcon(_ sender: UIButton) {
selectedIcon = sender
self.myDelegate?.changeFavoris(sender: self)
}
@IBAction func changeTitle(_ sender: Any) {
txtFavoriteNameInput.text = "gare centro"
print("delegate: ",myDelegate)
if myDelegate != nil {
myDelegate?.changeFavoris(sender: self)
}
}
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
}
}
使用该协议的第二类是
addFavoriteViewController: UIViewController {
// MARK properties
let defaultLocalizer = AMPLocalizeUtils.defaultLocalizer
var favorisName:String? = nil
var defaultIcon:FavIconType = .heart
var delegate:handleFavorite? = nil
@IBOutlet weak var favTableView: UITableView!
var headerCell:FavorisHeaderTableViewCell?
override func viewDidLoad() {
super.viewDidLoad()
// Localization of labels
//lblAddToFavorite.text = defaultLocalizer.stringForKey(key: "str_favorites_addTitle")
//lblFavoriteName.text = defaultLocalizer.stringForKey(key: "str_favorites_nameInput")
favTableView.delegate = self
favTableView.dataSource = self
self.headerCell?.myDelegate = self
}
override func viewWillAppear(_ animated: Bool) {
// Text Field
//favorisName.clearButtonMode = .whileEditing
}
var place:PlaceModel? = nil
var itinerary:(source:PlaceModel, destination:PlaceModel)? = nil
let db = DataController.shared
var favorite:FavoritesMO? = nil
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UGOaddFavoriteViewController: FavorisHeaderDelegate {
func changeFavoris(sender: FavorisHeaderTableViewCell) {
defaultIcon = sender.defaultIcon
favorisName = sender.txtFavoriteNameInput.text
}
}
当我尝试这段代码时,“myDelegate”总是为零而且我不明白什么是错的,尽管所有主题都是关于这个问题的。
答案 0 :(得分:1)
您将from tkinter import *
root = Tk()
def login(variable):
print("Login triggered")
login_button = Button(root, text="Login", command=lambda: login("variable"))
login_button.pack()
root.mainloop()
设置为错误单元格的委托!
下面:
self
您将self.headerCell?.myDelegate = self
的委托设置为headerCell
,但self
实际上从未显示在屏幕上!
您需要在屏幕上实际设置单元格的委托,而不是您创建的随机单元格的委托。
执行此操作的最佳位置是headerCell
:
cellForRowAtIndexPath