【问题标题】:Remove or Add UITableViewCell according to state of UISwitch根据 UISwitch 的状态移除或添加 UITableViewCell
【发布时间】:2011-11-23 16:59:47
【问题描述】:

我有一个有四行的表。最初应该显示其中的两个——第 1 行和第 2 行。另外两个应该仅在位于第 2 行的 UISwitch 的状态为“开”时出现。如果状态设置为“关闭”,那么它们应该会消失。

我创建了一个UITableViewCell,其中包含UISwitch

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSMutableArray *dict = [dictionary objectForKey:@"DATA"];

    //the location of the switch!

        if(indexPath.row == 1){

            SwitchCell *cell = (SwitchCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            if (cell == nil) {

                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwitchCell" owner:self options:nil];
                cell = (SwitchCell *)[nib objectAtIndex:0];
            }

            cell.title1.text = [dict objectAtIndex:indexPath.row];

            [cell.switchSpecifyEnd addTarget:self action:@selector(switchSpecifyEnd) forControlEvents:UIControlEventValueChanged];
            mySwitch = cell.switchSpecifyEnd;

                return cell;
        }else{
        static NSString *CellIdentifier1 = @"CustonCell";

        DateTimeCell *cell = (DateTimeCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DateTimeCell" owner:self options:nil];
            cell = (DateTimeCell *)[nib objectAtIndex:0];
        }

        cell.description.text = [dict objectAtIndex:indexPath.row];
        cell.dateTime.text = self.date;

        return cell;
    }
}



-(void) switchSpecifyEnd{
    //displays only 0 (defined initial state of  my switch)
    NSLog(@"SWITCH - Specify End: %d",  mySwitch.state);


    //    [tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //    [tableView1 reloadData];
}

如何让第 3 行和第 4 行最初不显示,并在开关状态更改为“开”时让它们出现?

【问题讨论】:

    标签: objective-c ios cocoa-touch uitableview uiswitch


    【解决方案1】:

    我认为最好的做法是:

    • UISwitch移出tableView,放在table上方即可
    • 使用NSMutableArray 作为数据源

    只需在数据源数组中添加或删除这两个元素,并在切换开关后调用[tableView reloadData]

    这意味着,与其只是让 tableViewCell 不可见,不如在每次想要隐藏它时删除它后面的整个数据。这可能不太理想,因此您也可以选择NSArray 来保存您的数据。您应该有一个全局 BOOL 变量来跟踪是否应该显示 2 个单元格,而不是从中删除/添加数据。在您的 UITableViewDataSource 方法中,将函数的内容包装在 if 子句中。例如- (NSInteger)tableView:numberOfRowsInSection:

    // assume we have a global BOOL showAllRows which is manipulated with the UISwitch
    // assume the data for the UITableView is loaded from an NSArray * data
    if (self.showAllRows)
        return self.data.count;
    else
        return self.data.count - 2; 
    

    那么您应该在- (UITableViewCell*)tableView:cellForRowAtIndexPath: 方法中执行相同的操作来配置您的单元格。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您需要在您的类中设置一个变量来指示开关是打开还是关闭。在 tableView:numberOfRowsInSection 中使用该变量来确定是否需要返回 2 或 4 作为单元格数。

      将开关切换到“开”后,您需要更改该变量并调用 reloadData(您的开关操作中已经有此调用,但已被注释掉)。

      然后表格视图控制器将再次调用 tableView:numberOfRowsInSection 并获取新的单元格数量。然后 tableView:cellForRowAtIndexPath: 将被再次调用并再次请求所有单元格,现在包括数字 3 和 4。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-27
        • 1970-01-01
        • 2014-04-30
        • 1970-01-01
        • 2018-04-04
        相关资源
        最近更新 更多