Swift-从自定义UITableViewCell调用UIViewController的func
我的问题是: 我有一个MainTableViewController,带有一个使用自定义UITableViewCells的表的出口。在MainTableViewController中,我还有一个用于UIView的插座,名为BlackView 我想做的是:在myCustomCell中,我想设置“BlackView.hidden=false”。我试图在MainTableViewController文件中使用“class func”,并从myCustomCell调用它,但它不起作用,因为当我将“class”放在“func”之前时,Xcode会停止识别BlackView 因此,我想调用MainTableViewController的函数,或者从.xib文件的.swift访问其出口 有人知道怎么做吗 这是我的.xib文件:Swift-从自定义UITableViewCell调用UIViewController的func,swift,xcode,swift2,Swift,Xcode,Swift2,我的问题是: 我有一个MainTableViewController,带有一个使用自定义UITableViewCells的表的出口。在MainTableViewController中,我还有一个用于UIView的插座,名为BlackView 我想做的是:在myCustomCell中,我想设置“BlackView.hidden=false”。我试图在MainTableViewController文件中使用“class func”,并从myCustomCell调用它,但它不起作用,因为当我将“cla
class myCustomCell: UITableViewCell {
@IBOutlet weak var commentTextView: UITextView!
override func awakeFromNib() {
commentTextView.delegate = self
super.awakeFromNib()
}
func textViewDidBeginEditing(textView: UITextView) {
MainTableViewController.hideBlackView(true)
}
func textViewDidEndEditing(textView: UITextView) {
var comment = commentTextView.text
}
}
以下是我的.xib文件的.swift:
class myCustomCell: UITableViewCell {
@IBOutlet weak var commentTextView: UITextView!
override func awakeFromNib() {
commentTextView.delegate = self
super.awakeFromNib()
}
func textViewDidBeginEditing(textView: UITextView) {
MainTableViewController.hideBlackView(true)
}
func textViewDidEndEditing(textView: UITextView) {
var comment = commentTextView.text
}
}
这是我的MainTableViewController:
class MainTableViewController: UIViewController
@IBOutlet weak var MyTable: UITableView!
@IBOutlet weak var BlackView: UIView!
override func viewDidLoad() {
BlackView.hidden = true;
MyTable.registerNib(UINib(nibName: "myCustomCell", bundle: nil), forCellReuseIdentifier: "myCustomCellID")
}
class func hideBlackView(setToHidden: Bool) {
if setToHidden == true {
BlackView.hidden = true
} else {
BlackView.hidden = false
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCustomCellID") as! PublishHeaderTableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
}
这是我的主要情节提要:
答案是授权 BlackView是一个将由操作系统创建的实例。出口是引用该实例的特殊属性(称为出口)。显示MainTableViewController时,操作系统将创建其实例 您可能希望使用实例方法而不是类方法来更改BlackView实例上的隐藏属性。为此,需要将MainTableViewController实例的引用传递给myCustomCell。这被称为委托,这就是ios编程和大多数MVC模型的工作方式 要执行此操作,请添加一个委托协议(仅在自定义单元格的定义上方是正常的),并向此类型的单元格添加一个弱变量:
// use a class protocol for delegates so weak properties can be used
protocol MyCustomCellDelegate: class {
func hideBlackView(setToHidden: Bool)
}
class MyCustomCell: UITableViewCell {
@IBOutlet weak var commentTextView: UITextView!
weak var delegate: MyCustomCellDelegate?
override func awakeFromNib() {
commentTextView.delegate = self
super.awakeFromNib()
}
func textViewDidBeginEditing(textView: UITextView) {
delegate?.hideBlackView(true)
}
func textViewDidEndEditing(textView: UITextView) {
var comment = commentTextView.text
}
}
然后,当您在cellForRowAtIndexPath中设置单元格时,将其转换为正确的单元格类型,在您给出的示例not PublishHeaderTableViewCell中应为MyCustomCell(还请注意,我已将自定义单元格类名改为以大写字母开头,这是ios开发中的行业标准)。最后,将委托设置为MainTableViewController的实例(在实例函数中称为“self”)
顺便说一句,在您的情况下,您只使用了一个单元,所以您可能不需要对单元进行排队和重用。您可以将所有这些都去掉,然后返回您在CellForRowatineXpath方法中创建的单元格的一个简单实例。无论如何,我将保留所有这些,以防您刚刚简化了堆栈溢出代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// you need to cast the cell to your custom class to use it
let cell = tableView.dequeueReusableCellWithIdentifier("myCustomCellID") as! MyCustomCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
// set the delegate
cell.delegate = self
return cell
}
最后,也是非常重要的一点,您需要声明MainTableViewController符合将使用它的协议,以便其他对象要委托给它的函数(方法)将成功。在您的情况下,它需要既符合我们上面写的MyCustomCellDelegate,又因为您将它用于tableView的数据源(对于cellForRowAtIndexPath和numberOfRowsInSection),您需要声明它符合UITableViewDataSource(您可能已经通过Interface Builder(故事板)完成了这项工作)…如果没有,可以在类定义中执行)
最后,我不确定在自定义单元格中的awakeFromNib方法中为UITextView设置委托是否合适。我知道这种方法并不总是有效。。在您的情况下,因为它位于插座上,我认为这是可以的,但我自己不太使用XIB文件,因此您可能希望打印到控制台,以确保每次都调用它,或者更多地研究问题。答案是委派 BlackView是一个将由操作系统创建的实例。出口是引用该实例的特殊属性(称为出口)。显示MainTableViewController时,操作系统将创建其实例 您可能希望使用实例方法而不是类方法来更改BlackView实例上的隐藏属性。为此,需要将MainTableViewController实例的引用传递给myCustomCell。这被称为委托,这就是ios编程和大多数MVC模型的工作方式 要执行此操作,请添加一个委托协议(仅在自定义单元格的定义上方是正常的),并向此类型的单元格添加一个弱变量:
// use a class protocol for delegates so weak properties can be used
protocol MyCustomCellDelegate: class {
func hideBlackView(setToHidden: Bool)
}
class MyCustomCell: UITableViewCell {
@IBOutlet weak var commentTextView: UITextView!
weak var delegate: MyCustomCellDelegate?
override func awakeFromNib() {
commentTextView.delegate = self
super.awakeFromNib()
}
func textViewDidBeginEditing(textView: UITextView) {
delegate?.hideBlackView(true)
}
func textViewDidEndEditing(textView: UITextView) {
var comment = commentTextView.text
}
}
然后,当您在cellForRowAtIndexPath中设置单元格时,将其转换为正确的单元格类型,在您给出的示例not PublishHeaderTableViewCell中应为MyCustomCell(还请注意,我已将自定义单元格类名改为以大写字母开头,这是ios开发中的行业标准)。最后,将委托设置为MainTableViewController的实例(在实例函数中称为“self”)
顺便说一句,在您的情况下,您只使用了一个单元,所以您可能不需要对单元进行排队和重用。您可以将所有这些都去掉,然后返回您在CellForRowatineXpath方法中创建的单元格的一个简单实例。无论如何,我将保留所有这些,以防您刚刚简化了堆栈溢出代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// you need to cast the cell to your custom class to use it
let cell = tableView.dequeueReusableCellWithIdentifier("myCustomCellID") as! MyCustomCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
// set the delegate
cell.delegate = self
return cell
}
最后,也是非常重要的一点,您需要声明MainTableViewController符合将使用它的协议,以便其他对象要委托给它的函数(方法)将成功。在您的情况下,它需要既符合我们上面写的MyCustomCellDelegate,又因为您将它用于tableView的数据源(对于cellForRowAtIndexPath和numberOfRowsInSection),您需要声明它符合UITableViewDataSource(您可能已经通过Interface Builder(故事板)完成了这项工作)…如果没有,可以在类定义中执行)
最后,我不确定在自定义单元格中的awakeFromNib方法中为UITextView设置委托是否合适。我知道这种方法并不总是有效。。在你的情况下,因为它是在一个插座上,我认为这是可以的,但我自己不太使用XIB文件,所以你可能想打印到控制台,以确保它是b