【问题标题】:how can I click button inside UI table view cell (custom cell ) and then goto other viewcontroller如何单击 UI 表格视图单元(自定义单元)内的按钮,然后转到其他视图控制器
【发布时间】:2015-08-01 11:32:22
【问题描述】:

我有MyTableViewCell 类和他们的.xib

在我的TableViewController 上想要单击单元格内的按钮,然后转到我的另一个UIViewControllerUITableViewDataSourceUITableViewDelegate

从这里

 class OverviewViewController: UITableViewController 
 { 

override func viewDidLoad() {
    super.viewDidLoad()

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

到这里

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

我用那个

         ....

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

     ....

我的 HistoryList 方法

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

  }

但是当转到HistoryController 我得到这个错误:

在展开可选值时意外发现 nil

MyTableView 无

【问题讨论】:

  • 你正在使用故事板?
  • 我还没有看到那个库@ido07
  • 你能给我任何关于开发像那个库这样的多页面的建议吗?
  • 在该库中,给出了 swift 的一个示例项目。请检查一下。
  • 您的问题解决了吗?

标签: ios swift segue tableviewcell


【解决方案1】:

您需要自定义单元格的委托

在你应该在单元格内的操作按钮中使用它之后

在您的 tableView 控制器中添加协议

在您的 tableView 控制器中将委托设置为单元格

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

   cell.delegate = self;

在你可以在你的 tableview 控制器中使用你的委托方法之后

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

P.S 对不起,我在 Objective-c 代码上的示例,但是,这是做你想做的事情的唯一正确方法。

【讨论】:

  • 你可以把它转换成 swift ı dont know Objective-c
【解决方案2】:

Swift 的解决方案:

您必须在包含 tableView 的 ViewController 中编写一个委托方法,如下所示:

protocol PushButtonDelegate {

    func pushBtn()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PushButtonDelegate {

}

在您的 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)
    }
}

下一步是在 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()
    }
}

最后一步是在 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

}

完成。如果您需要任何进一步的帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2015-10-06
    相关资源
    最近更新 更多