代码之家  ›  专栏  ›  技术社区  ›  user4487951

从另一个.m文件获取项目的高度

  •  0
  • user4487951  · 技术社区  · 11 年前

    我在UITableView中有一个自定义单元格。我用程序创建了一个高度为200、宽度为50的标签。当我做 NSLog customCell.m 标签的宽度和高度,它给了我w:50和h:200。但当我做 NSLog(NS日志) 在里面 mainViewController.m 高度和宽度为0。

    不知道为什么会这样。我需要在 主视图控制器.m

    这是我的代码:

    自定义单元格.m

    - (void)awakeFromNib {
    
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
        [self.label setBackgroundColor:[UIColor yellowColor]];
        [self.label setText:@"Hello"];
        [self.myView addSubview:self.label];
    
        CGRect myFrame = self.myView.frame;
        myFrame.size.height = self.label.frame.size.height;
    
        self.myView.frame = myFrame;
        NSLog(@"%f", self.label.frame.size.height);  // Results: 200.0000
    }
    

    主视图控制器.m

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        customCell *cellVC = [[cutsomCell alloc] init];
    
        NSLog(@"%f, %f", cellVC.label.frame.size.height, cellVC.frame.size.height); // Results: 0.0000, 44.0000
    }
    

    我设定了 myView 在故事板中设置为0。

    如果你不明白什么,请提问,我很乐意解释。

    使现代化

    这是我的代码:

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
        if (self) {
            self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
        [self.label setBackgroundColor:[UIColor yellowColor]];
        [self.label setText:@"Hello"];
        [self.myView addSubview:self.label];
     view
        }
        return self;
    }
    

    viewDidLoad

    [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"cellID"];
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *customCell = @"customCell";
    
        customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:customCell owner:nil options:nil] objectAtIndex:0];
        }
    
        return cell;
    }
    

    现在的问题是,当我运行应用程序时,没有显示任何单元格。但我确实得到了正确的NSLog。

    2 回复  |  直到 11 年前
        1
  •  1
  •   user3435374    11 年前

    在mainViewController中,您只是使用alloc和init以编程方式创建了cellVC,而不是像在customCell.m中那样从nib创建它,因此没有对其调用wakeFromNib方法。

    使现代化 改回使用xib中的自定义单元格(而不是registerCell)

     [self.tableView registerNib:[UINib nibWithNibName:@"CustomCellxib"
                         bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"CustomCellReuseID"];    // in the viewDidLoad of the mainViewController
    

    然后取消对单元格的排队-您实际上不需要检查单元格是否为空,因为现在可以保证取消排队返回单元格

     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCellReuseID" forIndexPath:indexPath];
    

    在tableview数据源方法中

    将代码当前保存在wakeFromNib中,以初始化从nib添加的自定义单元格。xib中的默认单元格在此完全初始化,因此您应该能够自定义自定义视图。您不再需要initWithIdentifer

    您可以选择实现表视图委托方法,如:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        // calculate the row height or return a constant
       return 200.0;
     }
    
        2
  •  0
  •   Vivek Seth    11 年前

    问题是-awakeFromNib不能在-init之后直接调用

    试试看:

    - (instancetype)init {
        self = [super init];
        if (!self) {
          return nil;
        }
    
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
        [self.label setBackgroundColor:[UIColor yellowColor]];
        [self.label setText:@"Hello"];
        [self.myView addSubview:self.label];
    
        CGRect myFrame = self.myView.frame;
        myFrame.size.height = self.label.frame.size.height;
    
        self.myView.frame = myFrame;
        NSLog(@"%f", self.label.frame.size.height);  // Results: 200.0000
    
        return self;
    }