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

iphone - 在旋转时重绘 UITableViewCells


                                            <p><p>我有自定义 <code>UITableViewCell</code> 以及页眉和页脚。它们中的每一个都有 <code>UITextField</code> 在排列的列中。这些列的位置是根据 <code>UITableView</code> 的宽度以百分比确定的。 (也就是说,它可以在横向打开时利用更宽的屏幕)。</p>

<p>我遇到的问题是当我在 ViewController 加载后旋转 View 时。 <code>UITableViewCell</code> 仍在使用旧的定位。</p>

<p>搜索完 SO,我现在已经实现了 <code>didRotateFromInterfaceOrientation:</code> 并在此方法中重新加载了数据。 </p>

<p>现在已经对页眉和页脚进行了排序。然而,<code>UITableViewCell</code>s 仍然是预先旋转的格式。有没有办法强制表格完全重绘其所有内容?</p>

<p>编辑:我的 <code>tableView:cellForIndexPath:</code> 代码:</p>

<pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @&#34;CustomCell&#34;;
    UITextField *TextField1, *TextField2, *TextField3, *TextField4, *TextField5;
    UITableViewCell *cell = ;

    float width = (self.tableView.frame.size.width - 68);

    if (cell == nil) {
      cell = [[ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

            TextField1 = [ initWithFrame:CGRectMake(((width*position1)+10.0), 10.0, (width*width1), 31.0)];
            TextField1.tag = 1;

            TextField2 = [ initWithFrame:CGRectMake((width*position2), 10.0, (width*width2), 31.0)];
            TextField2.tag = 2;

            TextField3 = [ initWithFrame:CGRectMake((width*position3), 10.0, (width*width3), 31.0)];
            TextField3.tag = 3;

                TextField4 = [ initWithFrame:CGRectMake((width*position4), 10.0, (width*width4), 31.0)];
            TextField4.tag = 4;

            TextField5 = [ initWithFrame:CGRectMake((width*position5), 10.0, ((width*width5)-40.0-editingWidth), 31.0)];
            TextField5.tag = 5;

            cell.accessoryType = UITableViewCellAccessoryNone;

            ;
            ;
            ;
            ;
            ;

            ;
            ;
            ;
            ;
            ;

      } else {

            TextField1 = (UITextField *);
            TextField2 = (UITextField *);
            TextField3 = (UITextField *);
            TextField4 = (UITextField *);
            TextField5 = (UITextField *);

      }

    // Configure the cell...

    // Clear cell contents
    TextField1.text = @&#34;&#34;;
    TextField2.text = @&#34;&#34;;
    TextField3.text = @&#34;&#34;;
    TextField4.text = @&#34;&#34;;
    TextField5.text = @&#34;&#34;;

    int sectionCount = 0;
    sectionCount = ;

    // Repopulate cells

    NSMutableArray *cellDataArray = ;

    TextField1.text = [ 1];
    TextField2.text = [ 2];
    TextField3.text = [ 3];
    TextField4.text = [ 4];
    TextField5.text = [ 5];

    }

    return cell;

}
</code></pre>

<p>我的 <code>didRotateFromInterfaceOrientation:</code> 方法:</p>

<pre><code>-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {

    ;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当您在表格 View 上调用 reload 时,将在您的数据源上调用 tableView:cellForRowAtIndexPath: 方法。由于您已经初始化了单元格,因此不会调用 if 语句中的代码,也不会重新定位文本字段。</p>

<p>最简单的解决方法是将文本字段代码移出 if 语句。我不确定这是否会导致明显的性能下降。</p>

<p>顺便说一句,您添加到单元格的 nameTextField、resistanceTextField 等未初始化,但我认为这只是从代码传输到 stackoverflow 时发生的混淆。我假设它应该是您要添加的 TextField1、TextField2 等。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 在旋转时重绘 UITableViewCells,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10031935/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10031935/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 在旋转时重绘 UITableViewCells