[英]How to manage rotations in a subclass of UITableViewCell
我在通用应用程序中有一个自定义UITableViewCell
,我必须为 iPhone 和 iPad 设置不同的框架,我必须考虑不同的方向,但无法识别设备的方向。 UIDeviceOrientation
是在错误的地方? UIDeviceOrientation
返回0
。 这是CustomCell.m
的代码
-(void) layoutCellPortrait{
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
//......frame subviews iPhone
}else{
//........frame subviews Ipad
}
}
-(void) layoutCellLandscape{
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
//.....frame subviews iPhone
}else{
//.......frame subviews Ipad
}
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];
NSLog(@"The orientation is è %d", deviceOrientation);//here return 0
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
[self layoutCellPortrait];
}
else if (deviceOrientation== UIInterfaceOrientationLandscapeRight){
[self layoutCellLandscape];
}
else if (deviceOrientation==UIInterfaceOrientationLandscapeLeft){
[self layoutCellLandscape];
}
[self.contentView addSubview:self.subviews];
}
return self;
}
在您的类中实现旋转委托,并且每当发生旋转时,以该方向作为输入参数调用单元格类中的方法(假设您正在调用表上的reloadData
,该表又调用该方法,该方法位于cellForRowAtIndexpath
方法中。这应在每次旋转中相应地重置框架。
更新:
在轮换委托中,
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
self.interfaceOrientation = orientation;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[self.tableView reloadData];
}
在 tabelView cellForRowAtIndexPath
方法中,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
//code..
[cell rearrangeSubviewsForOrientation:self.interfaceOrientation];
return cell;
}
在你的cell
类中添加这个,
- (void)rearrangeSubviewsForOrientation:(UIInterfaceOrientation)orientation {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
//iPhone
if(orientation == UIInterfaceOrientationPortrait || orientation== UIInterfaceOrientationPortraitUpsideDown) {
//portait
} else {
//landscape
}
} else {
//iPad
if(orientation == UIInterfaceOrientationPortrait || orientation== UIInterfaceOrientationPortraitUpsideDown) {
//portait
} else {
//landscape
}
}
}
试试这个
-(void)layoutSubviews{
--- your code ----
}
注意:这是针对 UITableViewCell 子类的类
如果你想检测类中的方向变化,它是像 UIViewController 这样的子类,下面有一个类似的方法叫做viewDidLayoutSubviews
- (void)viewDidLayoutSubviews{
--- your code ---
}
对于斯威夫特,如果您有海关UIView
是你内部UITableViewCell
没有AutoLayout
和要更新的方向变化的UI,你可以做到这一点里面UITableViewCell
自定义类:
override func layoutSubviews() {
super.layoutSubviews()
self.customView.layoutSubviews()
}
其实我也做过同样的事情。 首先,您需要为不同的方向制作两个 UITableViewCell.xib,然后在您的 viewController 中实现以下方法。
您不需要在 customCell.xib ViewController 中检测设备方向自动管理 tabelView 方向。
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone)
{
if(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}
else
{
}
[yourTable reloadData];
}
然后在你的 tabelView CellForRowAtIndexPath 方法中实现下面的代码
if(self.interfaceOrientation==UIInterfaceOrientationPortrait || self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
static NSString *CellIdentifier = @"Cell";
CategoryCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CategoryCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
}
else{
static NSString *CellIdentifier = @"Cell1";
CategoryCell1 *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CategoryCell1 alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
您需要查看的 2 件事是:
首先,检查设备是否确实旋转以使用
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ [self.tableView reloadData]; }
其次,在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中,使用 UIInterfaceOrientationIsLandscape(self.interfaceOrientation) 检查 GUI 状态以加载所需的布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.