菜鸟教程小白 发表于 2022-12-11 16:59:53

ios - 自定义 UitableViewCell 显示某些表格行的重复数据


                                            <p><p>自定义 UITableViewCell 遇到了一个非常奇怪的问题。</p>

<p>我使用 Storyboard原型(prototype)单元创建了一个自定义 UITableViewCell。
自定义单元格包含用于用户输入的 TextView 。
该表有 5 行。</p>

<p>当我尝试运行应用程序并在 textview 中输入文本时,它可以正常工作,直到第 3 行之后,第 4 和 5 行的 textview 会自动填充第 1 和第 2 行数据。</p>

<p>谁能帮帮我。</p>

<p>我想补充一些信息。我在控制台中打印了单元格并将日志显示为:</p>

<pre><code>Data for row 0, &lt;TableSampleApp.customcell: 0x7ff3c3c33570; baseClass = UITableViewCell; frame = (0 0; 375 301); clipsToBounds = YES; autoresize = W; layer = &lt;CALayer: 0x7ff3c3c1aac0&gt;&gt;

Data for row 1, &lt;TableSampleApp.customcell: 0x7ff3c3dd8a40; baseClass = UITableViewCell; frame = (0 301; 375 301); clipsToBounds = YES; autoresize = W; layer = &lt;CALayer: 0x7ff3c3dd85c0&gt;&gt;

Data for row 2, &lt;TableSampleApp.customcell: 0x7ff3c3f12e10; baseClass = UITableViewCell; frame = (0 602; 375 301); clipsToBounds = YES; autoresize = W; layer = &lt;CALayer: 0x7ff3c3f12300&gt;&gt;

Data for row 3, &lt;TableSampleApp.customcell: 0x7ff3c3c33570; baseClass = UITableViewCell; frame = (0 903; 375 301); clipsToBounds = YES; autoresize = W; layer = &lt;CALayer: 0x7ff3c3c1aac0&gt;&gt;

Data for row 4, &lt;TableSampleApp.customcell: 0x7ff3c3dd8a40; baseClass = UITableViewCell; frame = (0 1204; 375 301); clipsToBounds = YES; autoresize = W; layer = &lt;CALayer: 0x7ff3c3dd85c0&gt;&gt;
</code></pre>

<p>正如您在此处看到的,第 0 行和第 3 行以及第 1 行和第 4 行的 CALayer 和 TableSampleApp.customcell 具有相同的值这可能是什么原因?</p>

<p>这是 cellforRowAtIndexPath 的代码:</p>

<pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell
{

    tableView.registerNib(UINib(nibName: &#34;RiskAssessCell&#34;, bundle: nil), forCellReuseIdentifier: &#34;questionCustomCell&#34;)
    cell = tableView.dequeueReusableCellWithIdentifier(&#34;questionCustomCell&#34;, forIndexPath: indexPath) as! QuestionCustomCell

    return cell
}
</code></pre>

<p>自定义单元格中有一个 TextView 。在我运行应用程序并在 textview 中输入文本后,相同的文本会在单元格 3 和 4 中复制。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用以下代码创建单元格,</p>

<pre><code>let cellIdentifier = &#34;CustomCell&#34;
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! yourCustomCell
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 自定义 UitableViewCell 显示某些表格行的重复数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38390623/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38390623/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 自定义 UitableViewCell 显示某些表格行的重复数据