简体   繁体   中英

Remove a UITableViewCells button when clicked

I Have a button within a uitableview cell -

在此处输入图片说明

I have set it up to trigger a fmethod when clicked - (the function displays messages and resets the message count).

my code for this method is as follows -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Define Custom Cells

    static NSString *CellCountI =@"CellCount";

    UITableViewCell *cell;

    feedData *f = [self.HpFeedArray objectAtIndex:indexPath.section];

    //Comparison Strings
    NSString *count = @"Count";

    //If statement Cell Filters
    //If Count Cell
    if ([f.FeedGroup isEqualToString:count]) {

        cell = [tableView dequeueReusableCellWithIdentifier:CellCountI forIndexPath:indexPath];

        HP_Header_TableViewCell *hpTC = (HP_Header_TableViewCell *)cell;

        hpTC.buttonPressedSelector = @selector(buttonImpMsg);

        hpTC.buttonPressedTarget = self;

        [hpTC.msgsBtn setTitle: f.FeedTitle forState: UIControlStateNormal];

        return hpTC;
    }
}

The buttonImpMsg method is as follows -

- (void)buttonImpMsg
 {
    NSLog(@"Back Button Pressed!");

   [self removeBtn];
 }

I would like to hide the button when clicked - but I'm not sure how to reference it from the buttonImpMsg method?

Pass the sender to the selector:-

- (void)buttonImpMsg:(id)sender { 
     [sender removeFromSuperview];
} 

You could implement the delegate pattern, IMHO, it's the most proper way.

i think , better will be to make it hidden, if you want to again. – @pawan

I would also hide the button rather than remove it.

Try this implementation to hide the button, you can also do the same thing to hide this one after.

TableViewCell.h:

#import "TableViewCell.h"

@implementation TableViewCell

//.... Connect your action or set selector to this method:

- (IBAction)hideButton:(id)sender
{
    UIButton *button = (UIButton *) sender;

    // hide the button by using the setter
    button.hidden = YES;

    //.... Check if the delegate method has been implemented
    if ([_delegate respondsToSelector:@selector(hideButton)]) {
        [_delegate hideButton];
    }
}

@end

TableViewCell.h

//... Declare the delegate:
@protocol CellDelegate <NSObject>

- (void)hideButton;

@end

@interface TableViewCell : UITableViewCell

//... Add a delegate property:
@property (strong, nonatomic) id <CellDelegate> delegate;

@end

ViewController.m:

//... set self a the delegate of your cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
    cell.delete = self;

    return cell;
}

ViewController.m:

//... 
@interface ViewController () <CellDelegate>
//...

//... Implement the delegate method if you need to do stuff on the controller side
- (void)hideButton
{
    //do stuff here if needed
}

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