[英]how can I click button inside UI table view cell (custom cell ) and then goto other viewcontroller
我有MyTableViewCell
类和他们的.xib
在我的TableViewController
想要单击单元格内的按钮,然后转到我的其他UIViewController
, UITableViewDataSource
, UITableViewDelegate
由此
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")
}
MyTableViewCell
historyBtn
我用那个
....
cell.historyBtn.tag = indexPath.row
cell.historyBtn.addTarget(self, action: "getHistoryList", forControlEvents: UIControlEvents.TouchUpInside)
....
我的HistoryList方法
func getHistoryList()
{
presentViewController(HistoryController(), animated: true, completion: nil)
}
但是当转到HistoryController
时,出现此错误:
展开Optional值时意外发现nil
MyTableView无
您需要为您的自定义单元格委派
您应该在单元格内的操作按钮中使用它之后
在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
}
PS我很抱歉我的目标代码示例,但这是完成所需操作的正确方法。
Swift解决方案:
您必须在ViewController中编写一个包含tableView的委托方法,如下所示:
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
}
DONE。 如果您需要其他帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.