旋转自定义的UITableViewCell
-
21-09-2019 - |
题
我有一个自定义的UITableViewCell其中包含几个UIButtons。每个按钮的帧位置是相对于所述单元的宽度。我设置autoresizingMask = UIViewAutoresizingFlexibleWidth所以它会在应用程序与无论是在横向或纵向模式的装置开始适当调整单元宽度和按钮的位置。
的问题是,当设备从一个模式旋转到其它的,因为的UITableViewCell是可重复使用的按钮不调整位置。换句话说,细胞不被初始化基于新UITalbeView宽度,因为设备旋转之前细胞的功能initWithStyle被调用,而不是在设备旋转后再次调用。任何建议?
解决方案 2
花的研究时间(包括在这个网站的职位)之后,我无法找到任何解决方案。但是,一个灯泡点亮突然。该解决方案是非常简单的。只要检测设备方向是横向或纵向模式,并为每个不同的名称定义ReusableCellIdentifier。
static NSString*Identifier;
if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight) {
Identifier= @"aCell_portrait";
}
else Identifier= @"DocumentOptionIdentifier_Landscape";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
其他提示
由于的UITableViewCell也是一个UIView,可以重写SETFRAME方法。您的表视图每次旋转,此方法将被称为对于所有小区。
-(void)setFrame:(CGRect)frame
{
[super setFrame:frame];
//Do your rotation stuffs here :)
}
以前的答案有一个严重的问题。 您应该使用[UIApplication的sharedApplication] .statusBarOrientation而非[的UIDevice currebtDevice] .orientation因为设备的方向无关,与接口方向 - 设备方向是基于加速度传感器的物理旋转
在勾选答案就像在旧的iOS版本的魅力。对于iOS 6.0我使用的下一个代码:
static NSString *Identifier;
if (self.interfaceOrientation==UIInterfaceOrientationPortrait) {
Identifier=@"aCell_portrait";
}
else {
Identifier=@"DocumentOptionIdentifier_Landscape";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
您需要修正您细胞帧宽度(假设高度在纵向和横向模式相同)的方法cellForRowAtIndexPath
内。这就是在这里工作。我用来创建与IB定制TableViewCell它总是初始化人像320像素宽度。与限定它按预期的帧,即使该单元被从队列“重复使用”。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
// create cell here...
}
// Adjust cell frame width to be equal to tableview frame width
cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height);
...
}
我也有类似的问题,这篇文章帮我。在我来说,我有一个单独的文件中声明的自定义类,并在这个文件我在layoutSubviews
下面的代码:
//PORTRAIT CELL
if ([UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeLeft &&
[UIDevice currentDevice].orientation!=UIDeviceOrientationLandscapeRight)
{
//build the custom content views for portrait mode here
}
else
{
//build the custom content views for landscape mode here
}
然后,在我的视图控制器我只实现willAnimateRotationToInterfaceOrientation:
并发送reloadData
消息到我的表视图。
使用此我没有触摸cellForRow
方法。