繁体   English   中英

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

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

  • 我想要的是

    有一个tableview。我只想通过在TableViewCell中点击来隐藏UIButtonTypeContactAdd附件。

  • 我的问题

    当我轻按附件按钮A(我只在整个过程中轻按)时,它正确隐藏了。 但是,当我向下滚动表格视图时,我发现另一个附件按钮B被隐藏了。 快速滚动到表格视图的顶部后,按钮B的家伙又在那儿,而另一个按钮C隐藏了...

    很遗憾我不能在帖子中放图片。希望您能理解发生了什么。


  • 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;
    }

所有这些都是因为细胞重用机制吗?

对不起,我的英语不好。
谢谢!

您不能以这种方式使用表格。 您应该有一个带有对象的数据模型,该对象存储按钮附件按钮状态。

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];
}

这样的事情。

是的,这是由于单元重用而发生的。 您可能需要跟踪表格视图中的每个按钮,这可能是通过使其与数据源对齐来实现的。 通过使单元格数据源跟踪每个按钮的状态,可以轻松完成此操作。

暂无
暂无

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

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