重写UIView:initWithFrame并获得nil参数

时间:2011-01-20 13:08:15

标签: iphone objective-c xcode ios

我有一个从UIView扩展的类。我用它来创建自定义默认视图。我使用-(id)initWithFrame:(CGRect)frame;但是当我使用初始值设定项时,frame参数始终为null值。

以下是代码:

#import "SomeView.h"
@implementation POIView
@synthesize theButton;

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    NSLog(@"%@",frame);
  }
  return self;
}
@end

NSLog结果总是(null),这就是我调用初始化

的地方
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 static NSString *CellIdentifier = @"CELL_ID";
 const int THE_TAG         = 1000;

 UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   CGRect *rect = CGRectMake(0, 0, tableView.frame.size.width,tableView.rowHeight);
   someView = [[SomeView alloc] initWithFrame:rect];
 }else{
   someView = (SomeView *) [cell viewWithTag:THE_TAG];
 }
 return cell;
}

我现在在tableView上使用它,但我也使用了一些UILabel的视图。

2 个答案:

答案 0 :(得分:1)

frame是一个struct,它不是NSObject的子类。您可以按照以下方式显示帧值。

NSLog(@"%.2f %.2f %.2f %.2f",frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);

NSLog(@“%@”,aObj); 等于 NSLog(@“%@”,[aObj description]);

答案 1 :(得分:0)

我看到的问题是CGRectMake有返回类型CGRect而不是CGRect *。这一行:

CGRect *rect = CGRectMake(0, 0, tableView.frame.size.width,tableView.rowHeight);

应该是

CGRect rect = CGRectMake(0, 0, tableView.frame.size.width,tableView.rowHeight);