如何从 UITableViewCell 中的 UIButton 调用控制器中的方法
how to call method in controller from a UIButton in a UITableViewCell
我有一个 UITableView 和我自己的 UITableViewCells,其中包含一个 UIButton。 UITableView 在运行时添加到我的 ViewController 中。一切正常,我在单元格中看到按钮。当按下按钮时,我的操作在我的 UITableViewCell 中被调用。
现在。当在我的单元格中按下按钮时,如何在我的 ViewController 中调用方法?
在我的 UITableViewCell Class 我的单元格中有一个按钮:
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 中调用一个方法。我怎样才能做到这一点?
我没听懂你的问题?你在找这个吗
func buttonActionText(sender:AnyObject){
CGPoint buttonPosition=sender.convertPoint(CGPointZero, fromView: self.tableView)
NSIndexPath indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
}
一个非常简单的解决方案:-
- 只需在您的自定义单元格中声明委托方法。
- 使用View Controller设置Cell的delegate
cell.delegate=self;
- 现在在 Cell 的按钮操作上调用委托方法,并将 button.tag 作为对象发送(如果需要),它将直接调用已定义的视图控制器的委托方法。
只是不要从 UITableViewCell 添加目标 class 而是在 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)
}
创建 UITableviewCell 的子类。在我的代码中,我使用了 Question Cell
func buttonActionTimeline(sender: UIButton!)
{
var btn:UIButton=sender as UIButton
let point = tblVIew.convertPoint(CGPointZero, fromView: sender)
let indexPath : NSIndexPath = tblVIew.indexPathForRowAtPoint(point)!
let cell = tblVIew.cellForRowAtIndexPath(indexPath) as QuestionCell // Your custom cell class name
cell.loaddata() // add this method in to custom cell class
}
我有一个 UITableView 和我自己的 UITableViewCells,其中包含一个 UIButton。 UITableView 在运行时添加到我的 ViewController 中。一切正常,我在单元格中看到按钮。当按下按钮时,我的操作在我的 UITableViewCell 中被调用。
现在。当在我的单元格中按下按钮时,如何在我的 ViewController 中调用方法?
在我的 UITableViewCell Class 我的单元格中有一个按钮:
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 中调用一个方法。我怎样才能做到这一点?
我没听懂你的问题?你在找这个吗
func buttonActionText(sender:AnyObject){
CGPoint buttonPosition=sender.convertPoint(CGPointZero, fromView: self.tableView)
NSIndexPath indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
}
一个非常简单的解决方案:-
- 只需在您的自定义单元格中声明委托方法。
- 使用View Controller设置Cell的delegate
cell.delegate=self;
- 现在在 Cell 的按钮操作上调用委托方法,并将 button.tag 作为对象发送(如果需要),它将直接调用已定义的视图控制器的委托方法。
只是不要从 UITableViewCell 添加目标 class 而是在 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)
}
创建 UITableviewCell 的子类。在我的代码中,我使用了 Question Cell
func buttonActionTimeline(sender: UIButton!)
{
var btn:UIButton=sender as UIButton
let point = tblVIew.convertPoint(CGPointZero, fromView: sender)
let indexPath : NSIndexPath = tblVIew.indexPathForRowAtPoint(point)!
let cell = tblVIew.cellForRowAtIndexPath(indexPath) as QuestionCell // Your custom cell class name
cell.loaddata() // add this method in to custom cell class
}