简体   繁体   English

iPhone:根据情节提要正确地在UITableViewCell中隐藏附件按钮

[英]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. 有一个tableview。我只想通过在TableViewCell中点击来隐藏UIButtonTypeContactAdd附件。

  • My problem 我的问题

    When I tapped the accessory button A(which I only tapped in the whole procedure) , it hided correctly. 当我轻按附件按钮A(我只在整个过程中轻按)时,它正确隐藏了。 But when I scrolled down the tableview , I found another accessory button B was hided ridiculously. 但是,当我向下滚动表格视图时,我发现另一个附件按钮B被隐藏了。 After scrolling quickly to the top side of the tableview , the button B guy was there again , meanwhile another button C hided... 快速滚动到表格视图的顶部后,按钮B的家伙又在那儿,而另一个按钮C隐藏了...

    It's pity I can't put images in my post.Hope you can understand what happened. 很遗憾我不能在帖子中放图片。希望您能理解发生了什么。

  • Code
    tableView:cellForRowAtIndexPath: 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. 通过使单元格数据源跟踪每个按钮的状态,可以轻松完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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