我有一个 UITableViewController
,它显示自定义类型的 NSArray 的属性。在纵向时,我想显示三个标签(像列一样),但在横向时,我可以在 UITableViewCell
上放置更多标签,我想显示 5 个标签。
目前,我通过将 UILabels
放在 UITableViewCell
上并标记它们,以便我可以在 Storyboard编辑器中的原型(prototype)单元格上布置我的肖像单元格,以便我可以到达 subview 并在 cellForRowAtIndexPath
方法中设置文本。我开始走这条路是为了轻松使用我的标签的自动布局。
我发现了很多关于在方向改变时调整单元格上的项目大小/移动项目的建议,但没有找到如何更改可见 subview 数量的建议。我正在考虑尝试使用两种不同的单元格标识符(一种具有更多用于横向模式的标签)并根据 [UIDevice currentDevice].orientation
选择我想要的一种。我想还需要在旋转时强制重新加载单元格。
有没有更简单/更好的方法来做到这一点?
最佳答案
您走在正确的轨道上。只需开发 2 个具有唯一标识符的不同原型(prototype),根据当前方向在 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 重复用于在两个方向上使用的标签。
编辑:在方向改变时重新加载表格。
关于ios - 根据方向更改 UITableViewCell 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21952278/