IOS: 将 UITableView 的方向从水平方向更改为垂直方向
IOS: change orientation of UITableView from horizontal to vertical
我在一个项目中工作,我在其中使用 UITableView
来显示数据。我的项目的方向是风景。现在我想在我的项目中添加纵向模式,这样任何用户都可以在纵向和横向模式下使用应用程序。
是否有任何可用的代码或方法可以帮助我将 UITableView
的方向从水平更改为垂直??
我想要这样的方向
我相信您只需将项目设置为支持纵向。之后,您的 UITableView
将随着您旋转设备或模拟器而旋转。
我创建了一个简单的项目来对此进行测试。在此处检查 output。
请注意,有时视图会正确旋转,有时则不会。一定是一个错误。
只需开发 2 个具有唯一标识符的不同原型,根据当前方向在 cellForRowAtIndexPath 中提供它们。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:@"portraitCell" forIndexPath:indexPath];
// configure cell
return cell;
} else {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:@"landscapeCell" forIndexPath:indexPath];
// configure cell
return cell;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.tableView reloadData];
}
对于具有更多标签的单元格,您只需为这些标签创建额外的 IBOutlets,并将现有的 IBOutlets 重复用于在两个方向上使用的标签。
我在一个项目中工作,我在其中使用 UITableView
来显示数据。我的项目的方向是风景。现在我想在我的项目中添加纵向模式,这样任何用户都可以在纵向和横向模式下使用应用程序。
是否有任何可用的代码或方法可以帮助我将 UITableView
的方向从水平更改为垂直??
我想要这样的方向
我相信您只需将项目设置为支持纵向。之后,您的 UITableView
将随着您旋转设备或模拟器而旋转。
我创建了一个简单的项目来对此进行测试。在此处检查 output。
请注意,有时视图会正确旋转,有时则不会。一定是一个错误。
只需开发 2 个具有唯一标识符的不同原型,根据当前方向在 cellForRowAtIndexPath 中提供它们。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:@"portraitCell" forIndexPath:indexPath];
// configure cell
return cell;
} else {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:@"landscapeCell" forIndexPath:indexPath];
// configure cell
return cell;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.tableView reloadData];
}
对于具有更多标签的单元格,您只需为这些标签创建额外的 IBOutlets,并将现有的 IBOutlets 重复用于在两个方向上使用的标签。