gpt4 book ai didi

iPhone:根据 Storyboard正确隐藏 UITableViewCell 中的附件按钮

转载 作者:行者123 更新时间:2023-11-29 04:47:00 26 4
gpt4 key购买 nike

  • 我想要什么

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

  • 我的问题

    当我点击附件按钮A(我在整个过程中只点击过)时,它正确隐藏了。但是当我向下滚动桌面 View 时,我发现另一个附件按钮 B 被可笑地隐藏了。 快速滚动到桌面 View 的顶部后,按钮 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;
}

这一切都是因为cell复用的机制吗?

抱歉我的英语不好。
谢谢!

最佳答案

你不能这样使用表格。您应该有一个包含存储按钮附件按钮状态的对象的数据模型。

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

像这样。

关于iPhone:根据 Storyboard正确隐藏 UITableViewCell 中的附件按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com