简体   繁体   中英

iPhone: Hide an accessory button in UITableViewCell CORRECTLY based on storyboard

  • What I want

    There is a tableview.I just want to hide a UIButtonTypeContactAdd accessory by tapping it in the TableViewCell.

  • My problem

    When I tapped the accessory button A(which I only tapped in the whole procedure) , it hided correctly. But when I scrolled down the tableview , I found another accessory button B was hided ridiculously. After scrolling quickly to the top side of the tableview , the button B guy was there again , meanwhile another button C hided...

    It's pity I can't put images in my post.Hope you can understand what happened.

  • Code
    tableView:cellForRowAtIndexPath:

     static NSString *CellIdentifier = @"All Name Showing Table"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if(!cell.accessoryView){ UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; cell.accessoryView = button; } 

    - (IBAction)buttonTapped:(UIButton *)sender
    {
        UITableViewCell *tvc = (UITableViewCell *)[sender superview];
        NSString *peopleTapped = [NSString stringWithFormat:@"you have favored %@",tvc.textLabel.text];
        NSLog(@"%@",peopleTapped);

        sender.hidden = YES;
    }

Is all of this because of the mechanism of cell reuse?

Sorry for my poor English.
Thanks!

You can't use table this way. You should have a data model with object which store button accessory button state.

static NSString *CellIdentifier = @"All Name Showing Table";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(!cell.accessoryView){
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = button;
}

Model *model = [_array objectAtIndex:indexPath.row];
button.tag = [_array indexOfObject:indexPath.row];
button.hidden = model.hidden;

....
}


- (IBAction)buttonTapped:(UIButton *)sender
{
 Model *model = [_array objectAtIndex:sender.tag];
 model.hidden = YES;
 [table reloadData];
}

Something like this.

Yes, it's happening because of cell reuse. You need to be able to keep track of each button in the tableview, probably by aligning it with your data source. This could be easily accomplished by having your cell data source keep track of the state of each button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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