Iphone 如何更改旋转时UITableViewController的外观?
我目前尝试使用UITableViewController执行的任务是在纵向模式下有一列单元格行,在横向模式下有两列单元格行。这只是为了查看方便(使用可用的宽度空间查看更多行单元格),因此两个列单元格的格式相同。然而,我不知道如何实现这一点 所以,我的想法是让我的单元格自定义内容使用“cellforrowatinexpath”方法,并检查当前的屏幕模式。问题是我必须在“shouldAutorotateToInterfaceOrientation”设置一些标志,还是有一些设置 第二,在“shouldAutorotateToInterfaceOrientation”中只调用table reload就可以重画表的单元格了吗Iphone 如何更改旋转时UITableViewController的外观?,iphone,objective-c,rotation,uitableview,Iphone,Objective C,Rotation,Uitableview,我目前尝试使用UITableViewController执行的任务是在纵向模式下有一列单元格行,在横向模式下有两列单元格行。这只是为了查看方便(使用可用的宽度空间查看更多行单元格),因此两个列单元格的格式相同。然而,我不知道如何实现这一点 所以,我的想法是让我的单元格自定义内容使用“cellforrowatinexpath”方法,并检查当前的屏幕模式。问题是我必须在“shouldAutorotateToInterfaceOrientation”设置一些标志,还是有一些设置 第二,在“should
此外,我正在考虑制作不同的nib,并用IB设计我的电池。我想这是另一个问题,只是想知道这将如何影响解决方案 您必须在
cellForRowatineXpath
中检查当前方向,并正确配置单元格。您可以使用IB创建两个不同的单元格
此外,您还必须在其中一个用于旋转事件的回调中调用[myTableView reloadData]
(shouldAutorotateToInterfaceOrientation
或DidRotateFrominerFaceOrientation
)<每次调用[myTableView reloadData]
时,都会调用code>cellForRowAtIndexPath(适用于所有单元格)。
确保使用不同的标识符来重用单元格
编辑:这是我编写代码的方式:
将2个IBOutlet添加到.h文件:
IBOutlet MyCustomCell1 * customCell1;
IBOutlet MyCustomCell2 * customCell2;
在Interface Builder中,设置每个单元格的标识符属性,可能类似于cellIdentifier1
和cellIdentifier2
。确保IB中文件的所有者是您的数据源(实现cellforrowatinexpath
的位置)
cellforrowatinexpath
应如下所示:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft
|| [UIDevice currentDevice].orientation == UIDeviceOrientationLandscaperight)
{
//Landscape, lets use MyCustomCell2.
NSString * cellIdentifier2 = @"cellIdentifier2";
MyCustomCell2 * cell = (MyCustomCell2 *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
//We have to initialize the cell, we're going to use IB
[[NSBundle mainBundle] loadNibNamed:@"CustomCell2NibName" owner:self options:nil];
//After this, customCell2 we defined in .h is initialized from IB
cell = customCell2;
}
//setup the cell, set text and everything.
return cell;
}
else
{
//portrait case, the same as before but using CustomCell1
NSString * cellIdentifier1 = @"cellIdentifier1";
MyCustomCell1 * cell = (MyCustomCell1 *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
//We have to initialize the cell, we're going to use IB
[[NSBundle mainBundle] loadNibNamed:@"CustomCell1NibName" owner:self options:nil];
//After this, customCell1 we defined in .h is initialized from IB
cell = customCell1;
}
//setup the cell, set text and everything.
return cell;
}
}在
表格视图的代码中:cellforrowatinexpath:
,您可以使用以下选项检查当前方向:
if (self.interfaceOrientation == UIInterfaceOrientationPortrait ||
self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
// use a portrait cell
} else {
// use a landscape cell
}
另外,请确保从
shouldAutorotateToInterfaceOrientation:
返回YES
。您还应该在旋转后(在didRotateFromInterfaceOrientation:
)使用[tableView reloadData]重新加载tableView
代码>以确保使用了正确的单元格。我对标识符不太熟悉:)它们是什么?我在不知道的情况下做了一些简单的列表…为了加快单元渲染过程,可以使用[yourTable dequeueReusableCellWithIdentifier]创建单元。该选择器返回UITableviewCell(如果为nil),这是第一次创建单元格并对其进行初始化。查看或访问iOS参考指南以了解更多信息;)谢谢,我想到了:)现在,你能更详细地说明如何设置不同的标识符吗?我想我应该有两个单独的调用,从cellForRowAtIndexPath到dequeueReusableCellWithIdentifier?我的意思是,我应该检查当前方向,如果(模式=纵向),那么将通过potrait模式标识符的单元出列,否则使用横向模式标识符出列?看起来我必须全部重写CellForRowatineXpath方法。