gpt4 book ai didi

ios - 如何从 UITableViewCell 中的 UIButton 调用 Controller 中的方法

转载 作者:搜寻专家 更新时间:2023-11-01 05:58:49 25 4
gpt4 key购买 nike

我有一个 UITableView 和我自己的 UITableViewCells,其中包含一个 UIButton。 UITableView 在运行时添加到我的 ViewController 中。一切正常,我在单元格中看到按钮。当按下按钮时,我的操作在我的 UITableViewCell 中被调用。

现在。当在我的单元格中按下按钮时,如何在我的 ViewController 中调用方法?

在我的 UITableViewCell 类中,单元格中有一个按钮:

let framebutton_in_cell:CGRect=CGRectMake(0, 0, 40,40);
button_in_cell.frame=framebutton_in_cell;
button_in_cell.addTarget(self, action: "buttonActionText:", forControlEvents: UIControlEvents.TouchUpInside)
button_in_cell.tag = 1;
var image2 = UIImage(named: "book");
button_in_cell.setImage(image2, forState: .Normal)
var transparent2:UIColor=UIColor(red: 0, green: 0, blue: 0, alpha: 0.0);

contentView.addSubview(button_in_cell);

我有我的函数 buttonActionText:

func buttonActionTimeline(sender:UIButton!)
{
var btnsendtag:UIButton = sender
//do something
}

我的 UITableView 在运行时添加到我的 UIViewController:

 override func viewDidLoad() {
super.viewDidLoad()
let fSearchResultY:CGFloat=fButtonHeight+fButtonY;
searchResults.frame = CGRectMake(0, fSearchResultY, screenwidth, screenheight-fSearchResultY);
searchResults.delegate = self;
searchResults.dataSource = self;
searchResults.registerClass(CellResult.self, forCellReuseIdentifier: "Cell");
self.view.addSubview(searchResults);

现在我想在我的 Viewcontroller 中调用一个方法,而不是在我的 UITableViewCell 中调用一个方法。我怎样才能做到这一点?

最佳答案

只是不要从 UITableViewCell 类添加目标,而是将其添加到 cellForRowAtIndexPath 方法中

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! SomeCustomCell

cell.button_in_cell.addTarget(self, action: "viewControllerMethod:", forControlEvents: UIControlEvents.TouchUpInside)

}

关于ios - 如何从 UITableViewCell 中的 UIButton 调用 Controller 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257997/

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