我正在尝试在控制器的单元格中显示UILabel
文本(的子类UIControl
) 。tableview
我的代码如下:
在UIControl
标签 .h 文件中
#import <Foundation/Foundation.h>
@interface UIControlLabel : UIControl
{
UILabel *userNameLabel;
}
@property (nonatomic, retain) UILabel *userNameLabel;
@end
在UIControl.m
文件中
#import "UIControlLabel.h"
@implementation UIControlLabel
@synthesize userNameLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSLog(@"in init with frame method");
self.userNameLabel = [[UILabel alloc] init];
[self addSubview: userNameLabel];
}
return self;
}
@end
在 tableviewcontroller .m 文件中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";
int row = [indexPath row];
Answer *thisAnswer = [self.array objectAtIndex:row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
UIControlLabel *control = [[UIControlLabel alloc]initWithFrame:CGRectMake(35, 10,100, 10)];
control.userNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0];
control.tag = 2;
[cell.contentView addSubview:control];
}
UIControlLabel *thisControl = (UIControlLabel *)[cell.contentView viewWithTag:2];
thisControl.userNameLabel.text = [NSString stringWithFormat:@"%@",thisAnswer.userName];
return cell;
}
我的问题是单元格没有显示我上面设置的标签。我在这里错过了什么吗?