菜鸟教程小白 发表于 2022-12-13 09:53:32

ios - 重复添加 subview 到 `UITableViewCell`


                                            <p><p>我使用自定义编辑操作,然后在按下操作后通过 <code>indexPath</code> 编辑行</p>

<pre><code>-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewRowAction *DeleteButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@&#34;Delete&#34; handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
      UITableViewCell *cell = (UITableViewCell*);
      UISwitch *Switch = [ initWithFrame: CGRectMake(0,0,0,0)];
      ;
    }];

return @;
}
</code></pre>

<p>这会在按下 <code>Delete</code> 操作按钮的单元格中添加一个 <code>UISwitch</code>,<b>然而</b>它每 12 行左右重复一次;所有索引路径都不同<i>(0-1)-(0-2)..etc</i>.</p>

<p>我个人认为这是由于本行抓取单元格的方法造成的<br/>
<code>UITableViewCell *cell = (UITableViewCell*);</code></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>按照设计,当单元格从屏幕顶部滚动出来并且新单元格从底部向上滚动时,单元格会被重复使用。除非您明确选择退出此行为,否则它将准确解释您所观察到的内容。如果您在可见的单元格上设置任何属性(无论是标签上的文本,还是添加 subview ),则需要取消这些更改,以便在“重新使用单元格”时"或回收,修改在您不期望或不希望的上下文中仍然不可见。</p>

<p>解决此问题的一个方法是使用 <code>UITableViewCell</code> 的自定义子类,而不是将您的切换 subview 添加到标准 <code>UITableViewCell</code>。您将为自定义子类提供一个属性,其中引用您在 rowAction 方法中添加的 subview 开关。此引用使您能够在以后删除或隐藏它。</p>

<p>您可以考虑将开关设置为始终存在于您的自定义单元格的 <code>contentView</code> 中的 subview ,但它会根据操作方法的需要隐藏或显示。</p>

<p>在您的子类中,您将覆盖 <code>-prepareForReuse</code> 方法,并且您可以从自定义单元格的内容 View 中删除或隐藏 subview ,以准备单元格在新的上下文中呈现(例如当您不期望它时,没有添加的开关。)</p>

<p>创建自定义单元子类:</p>

<pre><code>@interface SampleCellTableViewCell : UITableViewCell

@property (weak, nonatomic) UISwitch *aSwitch;

@end

@implementation SampleCellTableViewCell

- (void)prepareForReuse
{
    [ removeFromSuperview];
    ;
}

@end
</code></pre>

<p>在您的操作方法中,分配属性:</p>

<pre><code>-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewRowAction *DeleteButton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@&#34;Delete&#34; handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
      SampleTableViewCell *cell = (SampleTableVieCell*);
      UISwitch *switch = [ initWithFrame: CGRectMake(0,0,0,0)];
      ;
      ;
    }];

return @;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 重复添加 subview 到`UITableViewCell`,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32098379/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32098379/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 重复添加 subview 到 `UITableViewCell`