UISegmentedControl没有响应
问题描述:
我以编程方式将UISegmentedControl添加到分组UITableView的脚。它显示正常,但是当我点击任何选项时,它们不会突出显示。我在这里做错了什么?另外,如何设置默认项目以突出显示?UISegmentedControl没有响应
- (void)viewDidLoad
{
[super viewDidLoad];
// set up cacheControl
NSArray* cacheDays = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", nil];
self.cacheControl = [[UISegmentedControl alloc] initWithItems:cacheDays];
[self.cacheControl setTintColor:[UIColor blackColor]];
[self.cacheControl addTarget:self action:@selector(cacheSelection:) forControlEvents:UIControlEventValueChanged];
self.cacheControl.frame = CGRectMake(10, 16, self.tbl.frame.size.width-20, 47);
[self.cacheControl setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
}
- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
switch (section) {
case 0:{
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.frame.size.width, tbl.contentInset.top)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}
case 1:{
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.frame.size.width, 16)];
footerView.backgroundColor = [UIColor clearColor];
[footerView setClipsToBounds:NO];
[footerView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[footerView addSubview:self.cacheControl];
return footerView;
}
}
return nil;
}
截图,根据要求:
答
要设置默认项,这样做:[cacheControl setSelectedSegmentIndex:0]
与您要选择的索引。
+0
我问了两个问题。我找到了我的第一个答案(我发表了评论),这确实回答了第二个答案。 – hodgesmr 2013-04-29 12:56:45
答
您应该执行UITableViewDelegate
协议中的-tableView:heightForFooterInSection:
。
这将让表视图调整节页脚的高度以适合您的视图并允许它接收用户交互。
你可以发布截图吗? – Undo 2013-04-28 00:05:31
当然。刚刚编辑。 – hodgesmr 2013-04-28 00:08:20
来自类似问题的这个答案为我修复了这个问题: http://stackoverflow.com/a/12436632/107821 – hodgesmr 2013-04-28 01:39:23