我有自定义 UITableViewCell
以及页眉和页脚。每个列中都有 UITextField
并排排列。这些列的位置是根据 UITableView
的宽度以百分比为基础确定的。 (也就是说,当横向打开时,它可以利用更宽的屏幕)。
我遇到的问题是在 View Controller 加载后旋转 View 。 UITableViewCell
仍使用旧的定位。
经过搜索,我现在已经实现了 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];
}
最佳答案
当您在 TableView 上调用重新加载时,将在数据源上调用 tableView:cellForRowAtIndexPath: 方法。由于您已经初始化了单元格,因此不会调用 if 语句中的代码,并且文本字段也不会重新定位。
最简单的解决方法是将文本字段代码移出 if 语句。我不确定这是否会导致性能明显下降。
顺便说一句,您添加到单元格中的 nameTextField、resistanceTextField 等未初始化,但我认为这只是从代码传输到 stackoverflow 时发生的混淆。我认为您要添加的应该是 TextField1、TextField2 等。
关于iphone - 旋转时重绘 UITableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031935/