单击时删除UITableViewCells按钮

时间:2014-04-07 13:30:42

标签: ios objective-c uitableview

我在uitableview单元格中有一个按钮 -

enter image description here

我已将其设置为在单击时触发fmethod - (该功能显示消息并重置消息计数)。

此方法的代码如下 -

- (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;
    }
}

buttonImpMsg方法如下 -

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

   [self removeBtn];
 }

我想在点击时隐藏按钮 - 但我不确定如何从buttonImpMsg方法中引用它?

2 个答案:

答案 0 :(得分:3)

将发件人传递给选择器: -

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

答案 1 :(得分:1)

您可以实施委托模式,恕我直言,这是最恰当的方式。

  

我认为,如果你想再次,最好将它隐藏起来。 - @pawan

我也会隐藏按钮而不是将其删除。

尝试使用此实现隐藏按钮,您也可以执行相同操作以隐藏此按钮。

  

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
}