簡體   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