【问题标题】:UITableViewCell - Adding subview while using an UITableViewCellStyleUITableViewCell - 在使用 UITableViewCellStyle 时添加子视图
【发布时间】:2011-07-05 14:14:32
【问题描述】:

所以我有一个 UITableView,它的所有单元格都使用 UITableViewCellStyleValue1 呈现。

这种风格在 cell.indentationLevel

中非常适合我

现在我需要在单元格中显示一个 UISwitch 而不是 detailTextLabel。我开始在 cellForRowAtIndexPath 工作,这是我的代码。

UISwitch *switchField = [[UISwitch alloc] init];
switchField.frame = CGRectMake(tableView.bounds.size.width - 194, 8, 94, 27);
switchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
switchField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[cell.contentView addSubview:switchField];

一开始效果很好,但每当我旋转设备时,Switch 的边界都保持不变。我认为通过向 cell.contentView 添加一个 UIView 可以在需要时进行对齐。

我错过了什么,最重要的是我应该做什么来实现我想要的?

谢谢。

【问题讨论】:

    标签: iphone objective-c ios ipad xcode4


    【解决方案1】:

    你可以试试:

    switchField.autoresizingMask = UIAutoresizingMaskFlexibleLeft | UIAutoresizingMaskFlexibleRight;
    

    或者只是其中之一:

    switchField.autoresizingMask = UIAutoresizingMaskFlexibleLeft;
    
    switchField.autoresizingMask = UIAutoresizingMaskFlexibleRight;
    

    如果做不到......你可以继承 UITableViewCell,将 swithField 设为全局并覆盖 layoutSubviews:

    - (void)layoutSubviews {
        [super layoutSubviews];
        switchField.frame = CGRectMake(tableView.bounds.size.width - 194, 8, 94, 27);
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      相关资源
      最近更新 更多