将分段控件插入标题视图时出现问题 [英] Problem in inserting segmented control to header view

查看:51
本文介绍了将分段控件插入标题视图时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表视图中添加UISegmentedControl.我的表格视图中有两个部分,我希望将分段控件放置在第二部分中.在我的实现中,我按如下方式重写viewForHeaderInSection.

I'm trying to add a UISegmentedControl in my tableview. I have two sections in my tableview and I want the segmented control to be placed in the 2nd section. In my implementation, I override viewForHeaderInSection as follows.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

if (section == 1)
{   
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(10, 0,tableView.bounds.size.width, 10)] autorelease];
    NSArray *segmentTextContent = [NSArray arrayWithObjects:NSLocalizedString(@"Singles", @""), NSLocalizedString(@"Everyone", @""),nil];
    UISegmentedControl *segmentedControl = [[[UISegmentedControl alloc] initWithItems:segmentTextContent] autorelease];
    segmentedControl.selectedSegmentIndex = 1;
    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.frame = CGRectMake(0, 0, tableView.bounds.size.width+10, 20);
    [segmentedControl addTarget:self action:@selector(loadTable:) forControlEvents:UIControlEventValueChanged];
    [headerView addSubview:segmentedControl]; 
    return headerView;
}
else
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(10, 0,tableView.bounds.size.width, 10)] autorelease];
    return headerView;
}

}

我的问题是,一旦我选择了一个特定的细分,它似乎就没有被选中.也就是说,它并没有像预期的那样变黑.我已将分段控件早些时候放在导航栏中,并且它按预期在选择后为分段着色.

My problem is that once I select a particular segment, it doesn't appear to be selected. i.e. it's not getting dark colored as expected. I have placed the segmented control in my navigation bar earlier and it colors the segment after selection as expected.

任何帮助将不胜感激.谢谢

Any help would be highly appreciated. Thanks

推荐答案

这是一个较晚的反应,但我遇到了同样的问题并找出了问题所在.在上面的代码中,每当tableView调用reloadData时,headerView都会重新创建(在我基于对segmentedControl的单击后重新使用数据后发生),因此分段控件将返回其原始状态,并且触摸显然没有反映.

This is a late reaction, but I ran into the same issue and figured out the problem. In the code above, the headerView is recreated everytime the tableView calls reloadData (which in my occurs after I resort the data based on a click on the segmentedControl), and therefore the segmented control goes back to its original state, and touches are not apparently reflected.

为解决此问题,我使segmentedControl成为了一个ivar,并检查它是否已经存在.如果是这样,那么只需致电

To solve this, I made segmentedControl an ivar, and check if it already exists. If so, then just call

[headerView addSubview: segmentedControl];

否则,请完成控件的整个设置.

otherwise do the whole setup of the control.

希望这会有所帮助.

这篇关于将分段控件插入标题视图时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆