【问题标题】:What subview are all the cell subviews located at? (In the custom cell class)所有单元格子视图位于哪个子视图? (在自定义单元类中)
【发布时间】:2015-03-25 07:29:52
【问题描述】:

我有一个自定义的UITableViewCell,其中有一个与之关联的类。

awakeFromNib,(自定义单元类)我做了一个for in循环:

for (id view in self.subview)
{
    if (view isKindOfClass:[UITextField class]) {
        UITextField *textField = (UITextField *)view;
        [textField setBackgroundColor:[UIColor redColor]];
    }
}

当我在模拟器上运行它时,textFields 背景颜色没有变化。

我很确定,我错的是:self.subview。我应该用什么来代替它?

【问题讨论】:

    标签: ios objective-c uitextfield custom-cell


    【解决方案1】:
    Give a tag to your view in the cell that contain textfield let suppose tag = 0
    
    for (UIView *view in [self viewWithTag:0].subviews)
    {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)view;
            [textField setBackgroundColor:[UIColor redColor]];
        }
    }
    

    希望对您有所帮助。

    【讨论】:

    • 我有超过 1 个单元格
    【解决方案2】:

    从 self.contentView 获取子视图 -

    试试下面的代码

    for (id view in self.contentView.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)view;
            [textField setBackgroundColor:[UIColor redColor]];
        }
    }
    

    【讨论】:

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