我遇到一个问题是我需要一个包含严重UISwitch的格式化网格,我的解决方案是将这些UISwitch放在每个UILabel上。
但是,这些开关无法正常工作。有什么建议吗?
以下是我的代码:
-(void) drawSummaryColumn : (UILabel *) myLabel :(NSString *) title
{
NSArray *sHeaders = @[NSLocalizedString(@"Tue", nil),
NSLocalizedString(@"Wed", nil),
NSLocalizedString(@"Thu", nil),
NSLocalizedString(@"Fri", nil),
NSLocalizedString(@"Sat", nil),
NSLocalizedString(@"Sun", nil),
NSLocalizedString(@"Total", nil)];
float basizSize = myLabel.frame.size.width;
CGRect r;
CGRect pos;
pos.origin.x =8;
pos.origin.y = 3;
UILabel *firstCol = [[UILabel alloc] initWithFrame:CGRectZero];
r.origin.x =0.5;
r.origin.y =0.5;
r.size = CGSizeMake(120, 40);
firstCol.text =title;
firstCol.font = [ UIFont fontWithName :@"Georgia-Bold" size : 17];
firstCol.backgroundColor = [UIColor whiteColor];
firstCol.textAlignment = NSTextAlignmentCenter;
firstCol.frame =r ;
[myLabel addSubview:firstCol ];
basizSize -= 120;
basizSize = (float) basizSize / 7;
for (NSString *label in sHeaders)
{
UISwitch *objSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
objSwitch.frame = pos;
[objSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
UILabel *columns = [[UILabel alloc] initWithFrame:CGRectZero];
columns.backgroundColor = [UIColor whiteColor];
columns.textAlignment = NSTextAlignmentCenter;
[columns addSubview:objSwitch];
r.origin.x += r.size.width + 0.5f;
r.size = CGSizeMake(basizSize, 40);
columns.frame =r ;
[myLabel addSubview:columns];
}
}
答案 0 :(得分:0)
试
myLabel.userInteractionEnabled = YES;
firstCol.userInteractionEnabled = YES;
columns.userInteractionEnabled = YES;
默认情况下,标签的userInteraction属性为false。因此,所有它的子视图(包括uiswitch)最初都将此属性设置为FALSE。父视图(uilabel)需要启用此属性,以便它的子视图也可以启用用户交互。