2

我有 customUITableViewCell以及页眉和页脚。UITextField它们中的每一个在排列的列中都有s。这些列的位置是根据UITableView. (即这样它在横向打开时会利用更宽的屏幕)。

我遇到的问题是当我在视图控制器加载后旋转视图时。UITableViewCells 仍然使用旧的定位。

搜索完之后,我现在已经didRotateFromInterfaceOrientation:在这个方法中实现并重新加载了数据。

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

编辑:我的tableView:cellForIndexPath:代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CustomCell";
    UITextField *TextField1, *TextField2, *TextField3, *TextField4, *TextField5;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

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

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

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

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

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

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

            cell.accessoryType = UITableViewCellAccessoryNone;

            [cell.contentView addSubview:TextField1];
            [cell.contentView addSubview:TextField2];
            [cell.contentView addSubview:TextField3];
            [cell.contentView addSubview:TextField4];
            [cell.contentView addSubview:TextField5];

            [TextField1 release];
            [TextField2 release];
            [TextField3 release];
            [TextField4 release];
            [TextField5 release];

        } else {

            TextField1 = (UITextField *)[cell.contentView viewWithTag:1];
            TextField2 = (UITextField *)[cell.contentView viewWithTag:2];
            TextField3 = (UITextField *)[cell.contentView viewWithTag:3];
            TextField4 = (UITextField *)[cell.contentView viewWithTag:4];
            TextField5 = (UITextField *)[cell.contentView viewWithTag:5];

        }

    // Configure the cell...

    // Clear cell contents
    TextField1.text = @"";
    TextField2.text = @"";
    TextField3.text = @"";
    TextField4.text = @"";
    TextField5.text = @"";

    int sectionCount = 0;
    sectionCount = [workoutSectionsMutableArray count];

    // Repopulate cells

    NSMutableArray *cellDataArray = [self retrieveCellDataAtIndexPath:indexPath];

    TextField1.text = [[cellDataArray objectAtIndex:indexPath.row] 1];
    TextField2.text = [[cellDataArray objectAtIndex:indexPath.row] 2];
    TextField3.text = [[cellDataArray objectAtIndex:indexPath.row] 3];
    TextField4.text = [[cellDataArray objectAtIndex:indexPath.row] 4];
    TextField5.text = [[cellDataArray objectAtIndex:indexPath.row] 5];

    }

    return cell;

}

我的didRotateFromInterfaceOrientation:方法:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {

    [self.tableView reloadData];
}
4

1 回答 1

3

当您在表视图上调用 reload 时,将在您的数据源上调用 tableView:cellForRowAtIndexPath: 方法。由于您已经初始化了单元格,因此不会调用 if 语句中的代码,并且不会重新定位文本字段。

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

顺便说一句,您添加到单元格的 nameTextField、resistanceTextField 等未初始化,但我认为这只是从代码传输到 stackoverflow 时发生的混淆。我假设它应该是您要添加的 TextField1、TextField2 等。

于 2012-04-05T20:44:09.263 回答