gpt4 book ai didi

ios - 如何单击 UI TableView 单元格(自定义单元格)内的按钮,然后转到其他 View Controller

转载 作者:行者123 更新时间:2023-11-28 09:02:00 24 4
gpt4 key购买 nike

我有 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")


}
MyTableViewCell 中的

historyBtn

我用那个

         ....

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

....

我的历史列表方法

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

}

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

unexpectedly found nil while unwrapping an Optional value

MyTableView 无

最佳答案

您需要为您的自定义单元格委托(delegate)

enter image description here

enter image description here

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

enter image description here

在您的 tableView Controller 中添加协议(protocol)

enter image description here

在您的 tableView Controller 中将委托(delegate)设置为单元格

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

cell.delegate = self;

在你可以在你的tableview controller中使用你的delegate方法之后

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

P.S 我很抱歉我在 objective-c 代码上的例子,但是,这是做你想做的唯一正确的方法。

关于ios - 如何单击 UI TableView 单元格(自定义单元格)内的按钮,然后转到其他 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com