简体   繁体   English

单击时删除UITableViewCells按钮

[英]Remove a UITableViewCells button when clicked

I Have a button within a uitableview cell - 我在uitableview单元格中有一个按钮-

在此处输入图片说明

I have set it up to trigger a fmethod when clicked - (the function displays messages and resets the message count). 我已将其设置为在单击时触发fmethod-(该函数显示消息并重置消息计数)。

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 - buttonImpMsg方法如下-

- (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? 我想在单击时隐藏按钮-但是我不确定如何从buttonImpMsg方法引用它?

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 – @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: 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 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: ViewController.m:

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

    return cell;
}

ViewController.m: 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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