I have a class that extend from UIView. I used that for creating custom default view. I use the -(id)initWithFrame:(CGRect)frame;
but when i used the initializer, the frame
parameter is always null
value.
Here is the code:
#import "SomeView.h"
@implementation POIView
@synthesize theButton;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSLog(@"%@",frame);
}
return self;
}
@end
The NSLog result is always (null)
, and this is where i called the initialization
-(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;
}
开发者_Go百科
I used it now on the tableView, but i used the someview to some UILabel too.
frame is a struct, it is not a sub-class from NSObject. You can show the frame value by following way.
NSLog(@"%.2f %.2f %.2f %.2f",frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
NSLog(@"%@", aObj); is equal to NSLog(@"%@",[aObj description]);
The problem I see is that CGRectMake has return type CGRect and not CGRect *. Thus the line:
CGRect *rect = CGRectMake(0, 0, tableView.frame.size.width,tableView.rowHeight);
should be
CGRect rect = CGRectMake(0, 0, tableView.frame.size.width,tableView.rowHeight);
精彩评论