【问题标题】:Table Cell not selectable after rotation旋转后无法选择表格单元格
【发布时间】:2013-12-22 10:50:13
【问题描述】:

我有一个视图,它在纵向和横向模式下应该有不同的布局。我实现了一种使用 Autosizing(无 AutoLayout)重绘 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 方法:返回最大尺寸属性(高度或宽度)。

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

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

【问题讨论】:

    标签: ios objective-c uitableview tableview autosize


    【解决方案1】:

    我的猜测是您的UITableView 的超级视图没有正确调整。输入永远不会到达您的UITableView,但是当它重新开始时一切都很好。可能是你UIViewController 的观点。

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

    【讨论】:

    • 如果我将此标志设置为我的超级视图 _tableContainerView 表不再可见。所以我假设我的 containerView 有问题。现在我正在寻找原因,但我找不到任何东西。我比较了我的容器和表格视图的反弹和帧,但它看起来像预期的那样。
    • 你能发布一个包含所有这些视图和一些虚拟数据的示例项目吗?
    • 好的,我发现了我的问题 - 自动调整蒙版大小。 willRotateToInterfaceOrientation: 方法在设备旋转之前执行。所以所有元素都在正确的位置。现在设备旋转,我的自动调整大小的面具改变了新情况的元素。不幸的是,我的容器视图非常小。感谢您的回答!
    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多