我有一个从 UIView 扩展的类。我用它来创建自定义默认 View 。我使用 -(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 上使用它,但我也将 someview 用于一些 UILabel。
最佳答案
frame 是一个结构体,它不是 NSObject 的子类。您可以通过以下方式显示帧值。
NSLog(@"%.2f %.2f %.2f %.2f",frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
NSLog(@"%@", aObj); 等于NSLog(@"%@",[aObj description]);
关于iphone - 覆盖 UIView :initWithFrame and got nil parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4747494/