簡體   English   中英

單擊時刪除UITableViewCells按鈕

[英]Remove a UITableViewCells button when clicked

我在uitableview單元格中有一個按鈕-

在此處輸入圖片說明

我已將其設置為在單擊時觸發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方法引用它?

將發件人傳遞給選擇器:-

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

您可以實現委托模式,恕我直言,這是最合適的方法。

我認為,如果您願意,最好將其隱藏起來。 – @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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM