ios - 在 InterfaceOrientation 上的 customCell 内重新定位 UIButtons

标签 ios objective-c uitableview uiinterfaceorientation

我正在使用带有 3 个按钮的自定义 tableviewcell。

我在自定义单元格中的 layoutSubviews 方法中设置了按钮的框架:

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGRect frame;
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
        frame = CGRectMake(310, 7, 55, 35);
        _prod.frame = frame;
        
        frame = CGRectMake(365, 7, 55, 35);
        _leva.frame = frame;
        
        frame = CGRectMake(220, 4, 65, 65);
        _imageButton.frame = frame;
    }
    else{
        frame = CGRectMake(150, 7, 55, 35);
        _prod.frame = frame;
        
        frame = CGRectMake(205, 7, 55, 35);
        _leva.frame = frame;
       
        frame = CGRectMake(120, 4, 65, 65);
        _imageButton.frame = frame;
    }
}

问题是,当用户滚动表格 View 时,在更改方向后,例如,所有按钮都回到原来的纵向位置。 下面是我如何初始化单元格:

LojaCell *cell = (LojaCell *) [tableView dequeueReusableCellWithIdentifier: @"LojaCell"];
if(cell == nil) cell = [[LojaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LojaCell"];

最佳答案

您可能必须将相同的方向敏感代码添加到 cellForRowAtIndexPath:,以便在 tableview 重新加载或重新创建单元格时它们加载到正确的位置。因为您正在使用 dequeueReusableCells,它会删除屏幕外的单元格以提高性能,这可能会解释滚动时的行为。

关于ios - 在 InterfaceOrientation 上的 customCell 内重新定位 UIButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15663370/

相关文章:

ios - UIImage imageWithContentsOfFile 中的路径参数 : for XCAsset

ios - Disqus 在 iOS UIWebView 上似乎比平时花费更长的时间

objective-c - 在 Objective-C 和 Cocoa 中,是否有一个函数 debug_print(foobar) 可以打印出变量的名称并转储它的值?

ios - 错误检查无法正常用于解析查询

ios - NSCalendar currentCalendar setTimeZone 未设置

ios - 解码动画Webp iOS时出错

iPhone - UITableView 中的 iAds

ios - 自定义 UITableViewCell 没有成员命名错误 Swift

uitableview - Swift - UITableView 中的褪色单元格

ios - UIView 子类快速访问 ViewController 方法