在旋转时重绘UITableViewCells

时间:2012-04-05 15:58:07

标签: iphone ios objective-c uitableview auto-rotation

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

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

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

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

编辑:我的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];
}

1 个答案:

答案 0 :(得分:3)

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

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

顺便说一下,你添加到单元格中的nameTextField,resistanceTextField等是未初始化的,但我认为这只是从代码转移到stackoverflow时发生的混乱。我假设它应该是你要添加的TextField1,TextField2等。