ios - 旋转后表格单元格不可选择

标签 ios objective-c uitableview tableview autosize

我有一个 View ,在纵向和横向模式下应该有不同的布局。我实现了一种使用自动调整大小(无自动布局)重绘 UI 元素的方法:

-(void)buildViewElements:(UIInterfaceOrientationMask)interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationMaskPortrait) {
    NSLog(@"Portrait");

    [UIView animateWithDuration:duration animations:^{
        CGRect newImageFrame = CGRectMake(20, 15, 100, 100);
        _personImage.frame   = newImageFrame;
        CGRect newNameLabelFrame = CGRectMake(128, 20, 172, 21);
        _nameLabel.frame = newNameLabelFrame;
        CGRect newNameDescFrame = CGRectMake(128, 41, 172, 21);
        _nameDescriptionLabel.frame = newNameDescFrame;
        CGRect newBorrowedFrame = CGRectMake(128, 64, 75, 21);
        _borrowedLabel.frame = newBorrowedFrame;
        CGRect newBorrowedValueFrame = CGRectMake(211, 64, 42, 21);
        _borrowedValueLabel.frame = newBorrowedValueFrame;
        CGRect newLendFrame = CGRectMake(128, 93, 75, 21);
        _lendLabel.frame = newLendFrame;
        CGRect newLendValueFrame = CGRectMake(211, 93, 42, 21);
        _lendValueLabel.frame = newLendValueFrame;

        CGRect newcontainerFrame = CGRectMake(0, 130, 320, [CNX_Tools getLargestAttributeOfSize:[[UIScreen mainScreen] bounds].size] - 194);
        _tableContainerView.frame = newcontainerFrame;
        CGRect newDetailFrame = CGRectMake(20, 0, 280, 29);
        _detailSelector.frame = newDetailFrame;
        CGRect newTableFrame = CGRectMake(0, 36, 320, newcontainerFrame.size.height - 39);
        _tableView.frame = newTableFrame;
    }];
}
else if (interfaceOrientation == UIInterfaceOrientationMaskLandscape) {
    NSLog(@"Landscape");

    [UIView animateWithDuration:duration animations:^{
        CGFloat factor = 1.0f;
        CGFloat diff   = 0.0f;
        if ([CNX_Tools getLargestAttributeOfSize:[[UIScreen mainScreen] bounds].size] <= 480) {
            // iPhone4 size
            factor =  0.8f;
            diff   = 10.0f;
        }

        CGRect newImageFrame = CGRectMake(75*factor-diff, 20, 100, 100);
        _personImage.frame = newImageFrame;
        NSLog(@"Bild Frame %f", newImageFrame.origin.x);
        CGRect newNameLabelFrame = CGRectMake(20, 140, 210*factor, 21);
        _nameLabel.frame = newNameLabelFrame;
        CGRect newNameDescFrame = CGRectMake(20, 161, 210*factor, 21);
        _nameDescriptionLabel.frame = newNameDescFrame;
        CGRect newBorrowedFrame = CGRectMake(20, 184, 75, 21);
        _borrowedLabel.frame = newBorrowedFrame;
        CGRect newBorrowedValueFrame = CGRectMake(103, 184, 42, 21);
        _borrowedValueLabel.frame = newBorrowedValueFrame;
        CGRect newLendFrame = CGRectMake(20, 213, 75, 21);
        _lendLabel.frame = newLendFrame;
        CGRect newLendValueFrame = CGRectMake(103, 213, 42, 21);
        _lendValueLabel.frame = newLendValueFrame;

        CGRect newContainerFrame = CGRectMake(250*factor, 0, [CNX_Tools getLargestAttributeOfSize:[[UIScreen mainScreen] bounds].size] - 250*factor, 268);
        _tableContainerView.frame = newContainerFrame;
        CGRect newDetailFrame = CGRectMake(20, 10, newContainerFrame.size.width - 40 , 29);
        _detailSelector.frame = newDetailFrame;
        CGRect newTableFrame = CGRectMake(0, 49, newContainerFrame.size.width, 219);
        _tableView.frame = newTableFrame;
    }];
}

}

该方法是从方法调用的:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

switch (toInterfaceOrientation) {
    case UIInterfaceOrientationPortrait:
        [self buildViewElements:UIInterfaceOrientationMaskPortrait duration:duration];
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        [self buildViewElements:UIInterfaceOrientationMaskPortrait duration:duration];
        break;
    case UIInterfaceOrientationLandscapeLeft:
        [self buildViewElements:UIInterfaceOrientationMaskLandscape duration:duration];
        break;
    case UIInterfaceOrientationLandscapeRight:
        [self buildViewElements:UIInterfaceOrientationMaskLandscape duration:duration];
        break;
    default:
        break;
}

}

方法getLargestAttributeOfSize:返回最大尺寸属性(高度或宽度)。

如果我以纵向方式启动 View ,我可以点击表格单元格并调用方法 tableView: didSelectRowAtIndexPath: 。旋转到横向模式后,所有单元格均不可选,并且我无法在表格中滚动。但是,如果我离开这个屏幕并再次调用 ist(在横向模式下),则可以选择相同的表格单元格,调用该方法,我可以在表格中漫步。如果我将设备旋转为纵向,我也会出现同样的错误行为。旋转设备后,我无法与表格及其单元格交互。

有人可以帮助我或者知道我的问题是什么吗? 谢谢!

最佳答案

我的猜测是您的 UITableView 的 super View 未正确调整。输入永远不会到达您的 UITableView,但是当它重新启动时一切都很好。可能是您的 UIViewController 的 View 。

尝试将 clipsToBounds:YES 设置为 super View ,看看您的 tableView 是否实际上超出了 View 范围。

关于ios - 旋转后表格单元格不可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728566/

相关文章:

ios - 使用 CNContacts (Swift) 实现搜索 Controller 时出现问题

发生自动锁定时,iOS 后台定位模式不起作用

ios - 实现 didReceiveRemoteNotification 以在推送到来时显示新的 viewController

ios - 无法在 tableViewCell 中放置图像

ios - NSURLConnection 接收数据的百分比

iphone - 使用大量内存的功能

ios - 无法将 UITableViewCell 设置为单元格

ios - AppLovin 横幅广告在 iPad 上崩溃

ios - NSURLSession 错误处理

ios - 使用重用标识符的 UITableView cellForRowAtIndexPath 问题