我想从自定义UITableViewCell中的按钮执行segue,但是当我按下该按钮时,我不知道如何访问单元格的内容。我意识到这可以通过didSelectRowAtIndexPath来完成,但是我有那个方法执行另一个函数,我想使用我在表格单元格中创建的按钮。
这是我的表演方法,我遇到了问题:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showComments"
{
let vc:CommentOnPostViewController = segue.destinationViewController as CommentOnPostViewController
var buttonPosition:CGPoint = sender?.convertPoint(CGPointZero, toView: self.feed) as CGPoint!
var path:NSIndexPath = self.feed.indexPathForRowAtPoint(buttonPosition) as NSIndexPath!
var postToCommentOn:PFObject = feedList[path.row] as PFObject
vc.post = postToCommentOn as PFObject
}
}
我在显示单元格时标记按钮,然后给它一个目标动作:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
// There is code that goes here for creating and displaying the cell.
cell.commentButton.tag = indexPath.row
cell.commentButton.addTarget(self, action: "addComment", forControlEvents: UIControlEvents.TouchUpInside)
}
以下是按下按钮时调用的操作:
func addComment()
{
self.performSegueWithIdentifier("showComments", sender: self)
}
感谢任何帮助。谢谢!
答案 0 :(得分:39)
使用Method创建一个协议,该协议将由您在TableViewController上定义的CustomCell的Delegate调用
//Pass any objects, params you need to use on the
//segue call to send to the next controller.
protocol MyCustomCellDelegator {
func callSegueFromCell(myData dataobject: AnyObject)
}
现在在UITableViewController上使用协议
class MyTableViewController : UITableViewController, MyCustomCellDelegator {
//The usual Defined methods by UIViewController and UITableViewController
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Set the CustomCell new Delegate
var cell = tableView.dequeueReusableCellWithIdentifier(customIdentifier) as MyCustomCell
cell.delagete = self
return cell
}
//MARK: - MyCustomCellDelegator Methods
func callSegueFromCell(myData dataobject: AnyObject) {
//try not to send self, just to avoid retain cycles(depends on how you handle the code on the next controller)
self.performSegueWithIdentifier("showComments", sender:dataobject )
}
}
在自定义单元格上定义委托,在新按钮内调用委托功能。
class MyCustomCell : UITableViewCell {
var delegate:MyCustomCellDelegator!
@IBOutlet weak var myButton:UIButton
@IBAction func buttonPressed(sender:AnyObject){
var mydata = "Anydata you want to send to the next controller"
if(self.delegate != nil){ //Just to be safe.
self.delegate.callSegueFromCell(mydata)
}
}
}
希望这可以清晰明了,让您在代码中理解和实现。
答案 1 :(得分:2)
我找到了一个更快的答案,可以通过动态单元格中的按钮将数据传递给新VC。
设置Tableview,自定义单元格(将单元格与创建的类连接并设置单元格标识符)并创建新VC并通过segue(也为segue设置标识符)将其连接到你当前的TableViewController
使用插座设置单元格(cellButton)
class CustomCell: UITableViewCell {
@IBOutlet weak var cellButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
TableViewController-Class中的CellForRowAt函数
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
//...Do stuff for your cell
cell.cellButton.tag = indexPath.row //or value whatever you want (must be Int)
cell.cellButton.addTarget(self, action: #selector(TableViewController.buttonTapped(_:)), for: UIControlEvents.touchUpInside)
}
设置由按钮目标调用的按钮方法
func buttonTapped(_ sender:UIButton!){
self.performSegue(withIdentifier: "customSegueIdentifier", sender: sender)
}
准备segue功能
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "customSegueIdentifier") {
if let destination = segue.destination as? YourNewViewController {
if let button:UIButton = sender as! UIButton? {
print(button.tag) //optional
destination.valueViaSegue = button.tag
}
}
}
}
希望这段代码可以帮助你们。继续编码;)
答案 2 :(得分:1)
这就是我所做的
在您的UITableViewCell类中,添加了以下内容
protocol MyCustomCellDelegator {
func callSegueFromCell(myData dataobject: AnyObject)
}
class MyUITableViewCell: UITableViewCell {
var delegate:MyCustomCellDelegator!
.......
@IBAction func buttonPress (_ sender: Any){
if(self.delegate != nil){
self.delegate.callSegueFromCell(myData: )
}
}
}
在UITableViewController中
class STVHomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MyCustomCellDelegator {
.....
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "MyIdentifier") as? MyIdentifierCell {
// configure your cell
cell.configureCell(myIdentifier:)
// This line is important
cell.delegate = self
return cell
} else {
return MyIdentifierCell()
}
}
func callSegueFromCell(myData dataobject: AnyObject) {
print(dataobject)
self.performSegue(withIdentifier: "goToNextVC", sender: dataobject )
}
}