iOS

这里写图片描述
isOn 显示第二个cell 否则不显示

// 设置允许休息 第二个cell显示不显示
- (IBAction)allowRestSwitchValueChanged:(UISwitch *)sender
{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation: UITableViewRowAnimationNone];

/**
 *  刷新机制:
 *  1.switch值一改变就触发这个方法,重新加载section
 *  2.加载section时候判断是不是index 2 section  第一个是0
 *  3.判断 如果switch isOn 就返回2 加载两个cell
 *  4.    如果switch 不是isOn 返回1  加载一个cell  第二个cell就没了
 *  5.
 *  @return
 */

}

// 设置返回的cell个数

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==2) {
    return self.restSwitch.isOn ? 2 : 1;
    }

    return [super tableView:tableView numberOfRowsInSection:section];
    }

原文地址:https://www.cnblogs.com/yuqingzhude/p/4978853.html