Set Invisible Button After UITableView Execution

1

I'm having a question in my UItableView, I put a button on the Cell of a TableView.

cell.btnDownload.tag = indexPath.row;
[cell.btnDownload addTarget:self action:@selector(btnDownloadClick:) forControlEvents:UIControlEventTouchUpInside];

Then to execute I created this method.

    -(void) btnDownloadClick:(UIButton *) sender{

    // realizar donload aqui //

    Faz o donwload aqui

    // depois de fazer o download buscar a tag e setar o botão invisivel //


    for (UITableViewCell *cell in self.tableView.visibleCells) {
        UIButton *button = [cell viewWithTag:sender.tag];
        if (ValorTag == 0){
          button.hidden = YES;
        }
    }

    // depois de esconder o botão vou setar em um [NSUserDefaults standardUserDefaults];  o ID. 

}

In this case I'm using my tableView gets all white after clicking the button, I do not know where I might be going wrong !!

You want to hide only the button that was clicked after the download.

To understand better it is as if it were the App Store that you have a button to download and after finishing it changes image or some.

Thank you guys.

    
asked by anonymous 23.05.2016 / 06:26

1 answer

0

Using tags to find views should be avoided as it leaves the code difficult to understand.

In your particular case, when viewWithTag: is called in the cell with the button tag, it can return any type of view, not necessarily a button. Especially when the clicked button is the first (indexPath.row == 0 == default tag of any UIView)

In addition, it is no use merely hiding the button when it is clicked, since the cells of the tableview can be reloaded, causing the button to return to the default state.

I suggest a different solution to the problem:

  • Create an array to keep button indexes hidden.
  • When initializing the cell in the -cellForRowAtIndexPath method: Consider the array to set the visibility of the button.
  • In the button callback, mark the index of the clicked button and reload the cell that the button belongs to.
  • Example:

    @interface ExampleViewController ()
    
    @property (nonatomic, strong) NSMutableArray<NSNumber *> *hiddenButtonIndexes;
    
    @end
    
    @implementation ExampleViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.hiddenButtonIndexes = [NSMutableArray new];
        ...
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ....
        BOOL downloadButtonHidden = [self.hiddenButtonIndexes containsObject:@(indexPath.row)];
        [cell.downloadButton setHidden:downloadButtonHidden];
    }
    
    - (void)downloadButtonTapped:(UIButton *)sender
    {
        [self.hiddenButtonIndexes addObject:@(sender.tag)];
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:sender.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }
    
    @end
    
        
    23.05.2016 / 08:50