Custom UITableViewCell and IBAction

倖福魔咒の 提交于 2020-01-09 19:38:10

问题


I have a custom UITableViewCell on which I have added a button, I have associated that button on an IBAction in my viewController. Now the problem that i am facing is how do I know from which cell that button was created. When I present my viewController which has a table in it and has multiple rows (custom UITableViewCell), now when the user presses the button the action is getting called, but how do I know which row was it.

Because based on the row index I need to store some value.

Edit: I have some clue on it now, but still I am not sure how will I do it, so it seems like on my tableViewController cellForRowAtIndexPath method I have to do something like this

[cell.button1 addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];

And then I have to write a method

-(IBAction) addToCart:(id) sender

But still what I don't know is how do i get the row index in my addToCart method. Appreciate your help.


回答1:


Ok, finally I got the answer, looking into different forums, people were suggesting to do something like this

in the custom table view controller in cellForRowAtIndexPath do this

cell.addToCart.tag = indexPath.row;
[cell.addToCart addTarget:self action:@selector(addToCart:)    
                               forControlEvents:UIControlEventTouchUpInside];

where addToCart is name of UIButton in my customUITableViewCell. It didn't seems to work for me. So this is what I did

-(IBAction) addToCart:(id) sender{
        NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
                    [[sender superview] superview]];
    NSLog(@"The row id is %d",  indexPath.row); 
 }

And then through interfacebuilder I associated the action of my button to addToCart IBAction on my table view controller.




回答2:


Much less hackerific.

[cell.button1 addTarget:self action:@selector(addToCart:event:) forControlEvents:UIControlEventTouchUpInside];


- (void)addToCart:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];

}



回答3:


The accepted answer does not work any more. Please refer to this post

It does it that way:

- (void)checkButtonTapped:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ...
    }
}



回答4:


Many of the developer used custom view to show custom cell on Table view for older version of iOS. If you are one of them, then you will have to face a problem that your button click action will no longer work with iOS7.

How to resolve this:

You have two options:

Option 1: Create the new lay out with new table cell instead of taking view. and put all layouts again in table cell.

I know, this will require a lot of effort.If you don't want to do this, we have a very small hack for it:option 2

Option 2: Create a IBOutlet for your button and add this button as a subview of your cell's content view.

[self.myCell.contentView addSubview:self.btn_click];

The above line of code will add btn_click as a subview of you content view. Now button click action should work.



来源:https://stackoverflow.com/questions/4103643/custom-uitableviewcell-and-ibaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!