UISegmentedControl不更新视图 [英] UISegmentedControl not updating view

查看:40
本文介绍了UISegmentedControl不更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Xcode 5中构建一个应用程序,并且遇到了UISegmentedControl的一些奇怪行为.

I am building an app in Xcode 5, and I ran into some strange behavior of UISegmentedControl.

有关我正在构建的应用程序的第一个信息:

我正在构建一个应用程序,我希望用户可以在该应用程序中向注册公司订购产品.作为一项额外服务,我希望允许他们查看所做的订单,甚至可以根据状态对订单进行过滤:所有订单,有效订单和已下达订单.订单显示在UITableView中,我在标题视图内创建了UISegmentedControl来过滤订单.当UISegmentedControlselectedSegmentIndex更改时,它将执行NSPredicate来过滤数组并仅显示所需的顺序.

I'm building an app in which I want to allow users to order products at registered companies. As an extra service, I want to allow them to see the orders they did, and even to filter the orders on their status: All orders, Active orders & Delivered orders. the orders are displayed in a UITableView, and I created a UISegmentedControl inside the header view to filter the orders. when the selectedSegmentIndex of that UISegmentedControl changes, it executes an NSPredicate to filter the array and display only the desired orders.

现在它的做工都很好,除了1两件事:在我当我选择另一段没有更新的观点.默认selectedSegmentIndex为'Active',因为用户可能对活动订单最感兴趣,但是当我将其更改为'All'时,表视图显示所有订单(因此谓词在起作用),但视图仍然存在在同一selectedSegmentIndex上.

now it's working all fine, except for 1 thing: The UISegmentedControl I have does not update it's view when I select another segment. It's default selectedSegmentIndex is 'Active', because users are probably most interested in the active orders, but when I change it to 'All', the tableview displays all orders (so the predicate is working), but the view remains on the same selectedSegmentIndex.

我做了很多研究来解决这个问题,但是没有答案可以解决我的问题..我的代码:

I did a lot of research to solve this but no answer solves my problem.. my code:

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

    if(section == 0) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 45)]; // x,y,width,height

        //label
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320, 20)];
        title.text = @"Kies een filter...";
        [headerView addSubview:title];

        //segmentedcontrol
        NSArray *itemArray = [NSArray arrayWithObjects: @"Alle", @"Actief", @"Afgehandeld", nil];
        control = [[UISegmentedControl alloc] initWithItems:itemArray];
        [control setFrame:CGRectMake(0.0f, 20.0f, 320.0, 35.0)];
        control.userInteractionEnabled = YES;
        control.tintColor = [UIColor blackColor];
        [control setEnabled:YES];
        [control addTarget:self action:@selector(changeFilter:) forControlEvents:UIControlEventAllEvents];
        [headerView addSubview:control];

        //label containing selected filter info
        UILabel *info = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 55.0f, 320, 20)];
        if ([selectedFilterInfo isEqualToString:@"Alle"]) {
            info.text = @"Alle orders:";
        }
        else if ([selectedFilterInfo isEqualToString:@"Actief"]) {
            info.text = @"Alle actieve orders:";
        }
        else if ([selectedFilterInfo isEqualToString:@"Afgehandeld"]) {
            info.text = @"Alle afgehandelde orders:";
        }
        [headerView addSubview:info];

        headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bar.png"]];
        return headerView;
    }
}

及其触发的动作:

- (void)changeFilter:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
if (segmentedControl.selectedSegmentIndex == 0) {
    selectedFilterInfo = @"Alle";
    filteredArray = nil;
    [segmentedControl reloadInputViews];
    [segmentedControl setSelectedSegmentIndex:0];
}

else if (segmentedControl.selectedSegmentIndex == 1) {
    NSPredicate *deliveredpredicate = [NSPredicate predicateWithFormat:@"SELF.delivered contains[c] %@", @"0"];
    NSMutableArray *notDeliveredArray = [NSMutableArray arrayWithArray:[sortedArray filteredArrayUsingPredicate:deliveredpredicate]];
    filteredArray = [NSMutableArray arrayWithArray:notDeliveredArray];
    selectedFilterInfo = @"Actief";
    [segmentedControl reloadInputViews];
    [segmentedControl setSelectedSegmentIndex:1];
}

else if (segmentedControl.selectedSegmentIndex == 2) {
    NSPredicate *deliveredpredicate = [NSPredicate predicateWithFormat:@"SELF.delivered contains[c] %@", @"1"];
    NSArray *deliveredArray = [sortedArray filteredArrayUsingPredicate:deliveredpredicate];
    filteredArray = [NSMutableArray arrayWithArray:deliveredArray];
    selectedFilterInfo = @"Afgehandeld";
    [segmentedControl reloadInputViews];
    [segmentedControl setSelectedSegmentIndex:2];
}
[self.tableView reloadData];

}

我没有包含numberOfCellsInSection等,因为tableview部分运行良好.唯一不起作用的是更新视图.

I did not include the numberOfCellsInSection etcetera because the tableview part is working perfectly. the only thing that's not working is updating the view.

任何解决方案将不胜感激,谢谢大家!

Any solution would be really appreciated, Thank you all in advance!!

推荐答案

reloadData将重新加载tableView,并且当您重新加载tableView时,所有headerView也将被重新加载.重新加载tableView后,将出现一个新的标头.您现在看到的UISegmentedControl并不是您所点击的UISegmentedControl.

reloadData will reload the tableView, and when you reload the tableView all headerViews will be reloaded too. When the tableView was reloaded there is a new header. And the UISegmentedControl you see now is not the one that you have tapped.

创建一个保存所选索引的ivar

Create an ivar that holds your selected index

@implementation .. {
    NSInteger selectedIndex;
}

创建视图时,还原保存的索引

when creating the view restore the saved index

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    /* ... */
    control = [[UISegmentedControl alloc] initWithItems:itemArray];
    control.selectedSegmentIndex = selectedIndex;
    /* ... */
}

在更改segmentedControl时保存索引

save the index when changing the segmentedControl

- (void)changeFilter:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    selectedIndex = segmentedControl.selectedSegmentIndex;
    if (segmentedControl.selectedSegmentIndex == 0) {
        selectedFilterInfo = @"Alle";
        filteredArray = nil;
    }
    /* ... */

    // reloads the table and all header views!
    [tableView reloadData];
}


顺便说一句.当您使用UISegmentedControls时,无需再次调用setSelectedSegmentIndex:,水龙头已经设置了索引.而且reloadInputViews对于UISegmentedControl完全没有用.但是我想这只是添加了代码,因为它没有用.不要忘记删除它;-)


And btw. when you use UISegmentedControls there is no need to call setSelectedSegmentIndex: on it again, the tap sets the index already. And reloadInputViews is totally useless for a UISegmentedControl. But I guess this code was just added because it didn't work. Don't forget to remove it ;-)

这篇关于UISegmentedControl不更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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