简体   繁体   English

更新节中用于段控制选择的行数

[英]update number of rows in section for segment control selection

Am new to swift programming. 快速编程新手。 I have a tableview with single custom cell.On top of the page i have a segment control. 我有一个带有单个自定义单元格的表格视图。在页面顶部,我有一个细分控件。 In my custom cell i have two labels and textfields. 在我的自定义单元格中,我有两个标签和文本字段。 When the page is loaded, first segment will be in selected state in segment control and table rows count should be 5. 加载页面时,第一个段将在段控件中处于选定状态,并且表行数应为5。

If am selecting the 2nd option in segment, I should load one more row ie 6th row and hide one textfield from second row. 如果要在段中选择第二个选项,我应该再加载一行,即第六行,并从第二行隐藏一个文本字段。 Am able to load the tableview with 5 rows. 能够加载5行的tableview。 And when user selects from segment am not able to reload the table with 6 rows. 并且当用户从段中选择时,无法重新加载具有6行的表。 Here is my code, 这是我的代码,

class FirstViewController: UIViewController, UITableViewDelegate,   UITableViewDataSource {

 let numberOfRowsAtSection: [Int] = [5, 6]
 var selectedOption: Bool = false
 override func viewDidLoad() {
        super.viewDidLoad()
        reportTable.delegate = self
        reportTable.dataSource = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var rows: Int = 0
    if tableView == self.reportTable && selectFromOptions.selectedSegmentIndex == 0 && selectedOption == true {
        selectedOption = false;
        if section == 0 {
            rows = 5
        }
    } else if selectFromOptions.selectedSegmentIndex == 1 && selectedOption == true {
        if section == 1 {
            rows = 6
        }
    }
return rows
}

@IBAction func optionChanges(sender: UISegmentedControl) {
    switch selectFromOptions.selectedSegmentIndex {
    case 0:
        selectedOption = true
        reportTable.reloadData()
    case 1:
        selectedOption = true
        reportTable.reloadData()

    default:
        break; 
    }
}

How can i achieve the above? 我该如何实现以上目标? Thanks in advance. 提前致谢。

You have a mistake in your code where you check the section if section == 1 . 您在检查if section == 1的部分if section == 1出错。 There is only ever one section and its index is always 0. You should be able to discover this by setting a breakpoint in selectedOption and stepping through to see what values are being passed in and what code path you go down. 永远只有一个部分,它的索引始终为0。您应该能够通过在selectedOption设置一个断点并逐步查看传递的值和失败的代码路径来发现这一点。

I think this should work: 我认为这应该工作:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var rows: Int = 0
    if tableView == self.reportTable && selectFromOptions.selectedSegmentIndex == 0 && selectedOption == true {
        selectedOption = false;
        if section == 0 {
            rows = 5
        }
    } else if selectFromOptions.selectedSegmentIndex == 1 && selectedOption == true {
        if section == 0 {
            rows = 6
        }
    }
    return rows
}

I don't have the full context of your code, but it seems like there may be some needless conditions in the above. 我没有您的代码的完整上下文,但似乎上面可能有一些不必要的条件。 Does this not produce the desired result? 这不会产生预期的结果吗?

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch selectFromOptions.selectedSegmentIndex {
    case 0:
        return 5
    case 1:
        return 6
    default:
        return 0
    }
}

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

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