[英]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
想要单击单元格内的按钮,然后转到我的其他UIViewController
, UITableViewDataSource
, UITableViewDelegate
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.