简体   繁体   中英

How to get the button's reference in the cell as a subview when we have indexpath for tableview cell on long press gesture in ios

I am new to iOS. I'm adding a button in a cell as sub-view like:

UIButton *updateProfilebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    updateProfilebtn.frame = CGRectMake(200.0f, 5.0f, 30.0f, 30.0f);
    [updateProfilebtn setTitle:@"U" forState:UIControlStateNormal];

     updateProfilebtn.tag = @"update",indexPath.row;
    [cell.contentView addSubview:updateProfilebtn];
    [updateProfilebtn addTarget:self
                        action:@selector(updateProfile:)
                        forControlEvents:UIControlEventTouchUpInside];
updateProfilebtn.hidden = YES).

As you can see I have set visibility hidden, because on a long press I need to show the button.

But the problem is how do I do this? How do I get the reference of this button in a cell on longpress of the uitableview cell.

Also I want to show particular button for particular cell row not for all rows..

Any Ideas?

You can get the Indexpath in a method as follow

UITableViewCell *cell;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        cell = (UITableViewCell*)[sender superview];
    }
    else
    {
        cell = (UITableViewCell*)[[sender superview] superview];
    }

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

Here you get indexpath.

Edited : Also you can get it by following code :

UIButton *btn = (UIButton*)sender;
NSIndexPath *indexPath = nil;
indexPath = [tableview indexPathForItemAtPoint:[tableview convertPoint:btn.center fromView:[btn superview]]];

You get your Indexpath here.

You can add the indexPath.row as the UIButton tag.

myButton.tag = indexPath.row;

When you want to reference that button you can do it like this:

(UIButton *)[self.view viewWithTag: indexPath.row]

so you can get the button only for the cell you want.

This try:

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankAction:)];
    tgr.delegate = self;
    [self.view addGestureRecognizer:tgr];
}

You should subclass UITableViewCell and create a property for updateProfilebtn. Then do something like:

self.updateProfilebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.updateProfilebtn.frame = CGRectMake(200.0f, 5.0f, 30.0f, 30.0f);
[self.updateProfilebtn setTitle:@"U" forState:UIControlStateNormal];

[self.contentView addSubview:_updateProfilebtn];
[self.updateProfilebtn addTarget:self
                        action:@selector(updateProfile:)
                        forControlEvents:UIControlEventTouchUpInside];
self.updateProfilebtn.hidden = YES

This way, the cell always holds a reference to the button.

Now you can add a gesture recogniser after you set up the button, and have that gesture recogniser call a method within your cell subclass. Your view controller then doesn't need to keep a reference to the buttons in each cell.

UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
longPressGestureRecognizer.delegate = self;
[self addGestureRecognizer:longPressGestureRecognizer];

Then, still in your UITableViewCell subclass

-(void) handleLongPressGesture:(id)sender {
    self.updateProfilebtn.hidden = NO;
}

If you need to call back to your view controller, look into using a delegate.

I ll list out the steps you can do

  1. Get the location of longPress gesture recognizer. Inn the longPress selector method do,

    CGPoint point = [gesture locationInView:self.view];

  2. No get the tableViewCell from using this point. For that you could use indexPathForRowAtPoint to get indexPath and further get the cell using that indexPath.

  3. Now you have the cell contains the button you are looking for, use viewWithTag to get the button.

    UIButton *btn = [cell.contentView viewWithTag: indexPath.row]

Note: These are just steps which leads to the solution. If you have any doubts, can ask :)

Change button Tag in following method.

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    updateProfilebtn.tag = (indexPath.row + 1);
    }

put following code in viewDidLoad method

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 2.0; //seconds
    lpgr.delegate = self;
    [self.myTableView addGestureRecognizer:lpgr];

get your button object in following method

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.myTableView];

    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    } else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"long press on table view at row %d", indexPath.row);

    } else {
        NSLog(@"gestureRecognizer.state = %d", gestureRecognizer.state);
    }

    UIButton * btnTemp = (UIButton *)[self.myTableView viewWithTag:(indexPath.row + 1)];
    NSLog(@"your button = %@",btnTemp);



}
    #import "ViewController.h"
    @interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
        UITableView* _tableView;
    }
    @end
    @implementation ViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }

    - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 10;
    }

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellID"];
        // add gesture to cell
        UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = 1.0; //seconds
        [cell addGestureRecognizer:lpgr];
        UIButton* updateProfilebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        updateProfilebtn.frame = CGRectMake(200.0f, 5.0f, 30.0f, 30.0f);
        [updateProfilebtn setTitle:@"U" forState:UIControlStateNormal];
        updateProfilebtn.tag = indexPath.row;
        [cell.contentView addSubview:updateProfilebtn];
        [updateProfilebtn addTarget:self
                             action:@selector(updateProfile:)
                   forControlEvents:UIControlEventTouchUpInside];
        updateProfilebtn.hidden = YES;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

    - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
    {
        UITableViewCell* selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        UIButton* showBtn = [selectedCell.contentView viewWithTag:indexPath.row];
    }
    - (void)updateProfile:(UIButton*)btn
    {
        NSLog(@"%ld", (long)btn.tag);
    }

    -(void)handleLongPress:(UILongPressGestureRecognizer *)longPress
    {
        CGPoint p = [longPress locationInView:_tableView];
        NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:p];
        UITableViewCell* selectedCell = [_tableView cellForRowAtIndexPath:indexPath];

        UIButton* showBtn = [selectedCell.contentView viewWithTag:indexPath.row];
        NSLog(@"%ld", [showBtn tag]);
        showBtn.hidden = NO;

        }
    }

    @end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM