简体   繁体   English

如何在Navigationbar中更改UISwitch的状态时更改UITableView单元格?

[英]How to change UITableView cellrows on state change of UISwitch in Navigationbar?

I am having two methods in my UITableViewController class. 我的UITableViewController类中有两个方法。

[self itemProcessor]; //First Method
[self listAllItems];  //Second Method

First method will get called by default from my viewDidLoad and the cellForRowAtIndexPath gets populated based on the query from my first method. 默认情况下,第一个方法将从我的viewDidLoad调用,并且cellForRowAtIndexPath将根据第一个方法中的查询进行填充。 I have a Programmatically created UISwitch in my viewDidLoad like the below 我在viewDidLoad有一个以编程方式创建的UISwitch ,如下所示

UISwitch *barSwitch = [[UISwitch alloc] init];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barSwitch];
    [barSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; //To track switch state.

- (void)setState:(id)sender
{
    BOOL state = [sender isOn];
    NSString *rez = state == YES ? @"YES" : @"NO";
    NSLog(@"state is %@",rez);

}

I should call secondMethod( [self listAllItems]; ) when the switch is off. 当开关关闭时,我应该调用secondMethod( [self listAllItems]; )。 I have all my arrays getting initialized in my viewDidLoad . 我所有的数组都在viewDidLoad初始化。 How do i call two different queries based on the state change when i create the switch programmatically inside viewDidLoad ? 当我在viewDidLoad以编程方式创建开关时,如何根据状态变化调用两个不同的查询?

have a NSMutableArray , which you will use in viewDidLoad first, then update that array inside setState function and call tableView.reloadData() 有一个NSMutableArray,您将首先在viewDidLoad中使用它,然后在setState函数中更新该数组并调用tableView.reloadData()

- (void)setState:(id)sender
{
    BOOL state = [sender isOn];
    NSString *rez = state == YES ? @"YES" : @"NO";
    NSLog(@"state is %@",rez);

    if (state == YES) {
        // 
    }else{
        // myArray is declared in the .h file 
        myArray = [NSMutableArray arrayWithObjects:
                          @"str1", @"str2",
                          @"etc", nil];
        [self.tableView reloadData]; 
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM