Set invisible button after running Uitableview

Asked

Viewed 263 times

1

I have 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 I am using my tableView gets all white after clicking the button, do not know where I may be missing!!

Want to hide only the button that was clicked after the download.

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

Thank you guys.

1 answer

0


Using tags to find views should be avoided as it makes code hard to understand.

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

Also, it is no use just hiding the button when it is clicked, since the tableview cells can be reloaded, causing the button to return to the default state.

I suggest a different solution to the problem:

  1. Create an array to keep the indexes of hidden buttons.
  2. When initializing the cell in the -cellForRowAtIndexPath method: consider the array to set the button visibility.
  3. In the callback of the button, check the index of the button clicked and reload the cell to which the button belongs.

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
  • Opa Rafael thanks so much for the help, really I walking and searching for the tag to be hidden is hiding everything, ex, all the components were set with tag = 0. i had only one problem in setting in the Array that the value was not saved, when I closed the App and reopened the value was no longer set, but I used the Nsuserdefaults. and another problem that could have in the future is the reference of Indexpath.Row with the bank ID. Thanks for the help went all right. Great Abs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.