简体   繁体   English

我如何单击UI表格视图单元格(自定义单元格)内的按钮,然后转到其他ViewController

[英]how can I click button inside UI table view cell (custom cell ) and then goto other viewcontroller

I have MyTableViewCell class and their .xib 我有MyTableViewCell类和他们的.xib

on my TableViewController want to click button inside cell and then goto my other UIViewController , UITableViewDataSource , UITableViewDelegate 在我的TableViewController想要单击单元格内的按钮,然后转到我的其他UIViewControllerUITableViewDataSourceUITableViewDelegate

from this 由此

 class OverviewViewController: UITableViewController 
 { 

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "MyTableViewCell")
}

To this 对此

class HistoryController: UIViewController , UITableViewDataSource,   UITableViewDelegate{

@IBOutlet var MyTable: UITableView! 

override func viewDidLoad() {
    super.viewDidLoad()

    self.MyTable.registerNib(UINib(nibName: "HistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "HistoryTableViewCell")


}

historyBtn in MyTableViewCell MyTableViewCell historyBtn

an I use that 我用那个

         ....

     cell.historyBtn.tag = indexPath.row 
     cell.historyBtn.addTarget(self, action: "getHistoryList", forControlEvents: UIControlEvents.TouchUpInside)

     ....

My HistoryList Method 我的HistoryList方法

    func getHistoryList()
    {
     presentViewController(HistoryController(), animated: true, completion: nil)

  }

But when go to the HistoryController I get this error : 但是当转到HistoryController时,出现此错误:

unexpectedly found nil while unwrapping an Optional value 展开Optional值时意外发现nil

MyTableView nil MyTableView无

you need delegate for you custom cell 您需要为您的自定义单元格委派

在此处输入图片说明

在此处输入图片说明

after you should use it in action button inside cell 您应该在单元格内的操作按钮中使用它之后

在此处输入图片说明

in your tableView controller you add protocol 在tableView控制器中添加协议

在此处输入图片说明

in your tableView controller set delegate to cell 在您的tableView控制器中将委托设置为单元格

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

   cell.delegate = self;

after you can use your delegate method in your tableview controller 您可以在tableview控制器中使用委托方法之后

-(void)selectButtonWithDate:(NSDate *)date
{
 [self.navigationController pushViewController:picker animated:YES];//or another transition code
}

PS I'm sorry my example on objective-c code but , it is only right way to do what you want. PS我很抱歉我的目标代码示例,但这是完成所需操作的正确方法。

Solution for Swift: Swift解决方案:

You have to write a delegate method within your ViewController containing the tableView like this: 您必须在ViewController中编写一个包含tableView的委托方法,如下所示:

protocol PushButtonDelegate {

    func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

}

Within your ViewController you have to define the function pushBtn like this: 在您的ViewController中,您必须这样定义函数pushBtn:

protocol PushButtonDelegate {

     func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

     func pushBtn() {

        let stb = UIStoryboard(name: "Main", bundle: nil)
        let vc = stb.instantiateViewControllerWithIdentifier("UserSettingsVC")            
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

Next step is to call the delegate methos within your TableViewCell.swift like this: 下一步是像这样在TableViewCell.swift中调用委托方法:

import UIKit

class TableViewCell: UITableViewCell {

    var delegate : PushButtonDelegate?

    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
    }

    @IBAction func menuBtn(sender: AnyObject) {

        delegate?.pushBtn()
    }
}

The last step is to set the delegate in the cellForRowAtIndexPath function at ViewController like this: 最后一步是在ViewController的cellForRowAtIndexPath函数中设置委托,如下所示:

protocol PushButtonDelegate {

    func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

    func pushBtn() {

    let stb = UIStoryboard(name: "Main", bundle: nil)
    let VC4 = stb.instantiateViewControllerWithIdentifier("UserSettingsVC")
    VC4.title = "Settings"

    self.presentViewController(VC4, animated: true, completion: nil)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("tableViewCell") as! TableViewCell            
    cell.delegate = self     
    return cell

}

DONE. DONE。 If you need any further help, just let me know. 如果您需要其他帮助,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速在 ui 表视图单元格上创建自定义单选按钮 - how to create custom radio button on ui table view cell in swift 如何在按钮单击同一单元格内时隐藏/显示自定义tableview单元格内的特定视图 - how to hide/show a specific view inside the custom tableview cell when button click inside the same cell 如何将数据从弹出窗口内 uicollectionview 中的按钮单击发送到主视图控制器中的表格单元格? - How do I send data from a button click in a uicollectionview inside of a popover to a table cell in a main view controller? 单击按钮后如何显示表格视图单元格的值? - How can I show table view cell values after click button? 如何在表格视图单元格中单击按钮时获取标签值 - How to get label value in table view cell on button click in cell 在UI表格视图单元格中单击获取图像 - Get image click inside UI table view cell 表格视图的“自定义”单元格中的按钮 - Button in Custom cell of table view 带按钮的自定义表格视图单元格 - Custom table view cell with button 当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController - how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell 如何将 UITapGestureRecognizer 添加到表视图单元格内的 UILabel? - How can I add a UITapGestureRecognizer to a UILabel inside a table view cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM