隐藏搜索栏上的索引为1

时间:2014-10-04 09:09:43

标签: ios objective-c iphone uitableview swift

我有一个UITableView,其中searchBar在tableViews HeaderView中,当段= 1时我想删除它,但是当我试图隐藏它时,searchBar消失了,但是单元格没有移动到顶部。我怎么能解决这个问题。这是一张图片:

enter image description here

@IBAction func segmentAction(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        tableView.scrollEnabled = true
        segment = 0
        self.tableView.tableHeaderView?.hidden = false
    case 1:
        tableView.scrollEnabled = false
        segment = 1
        self.tableView.tableHeaderView?.hidden = true


    default:
        break
    }
    tableView.reloadData()

}

3 个答案:

答案 0 :(得分:2)

试试这段代码。它为我工作

@IBAction func segmentAction(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
    tableView.scrollEnabled = true
    segment = 0
    self.tableView.tableHeaderView = nil
case 1:
    tableView.scrollEnabled = false
    segment = 1
    self.tableView.tableHeaderView = searchBar


default:
    break
}
tableView.setNeedsDisplay()

}

答案 1 :(得分:0)

您尝试此代码

@IBAction func segmentAction(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        tableView.scrollEnabled = true
        segment = 0
        self.tableView.tableHeaderView?.hidden = false
    case 1:
        tableView.scrollEnabled = false
        segment = 1
        self.tableView.tableHeaderView?.hidden = true
       tableView.reloadData()

    default:
        break
    }
    tableView.reloadData()

}

答案 2 :(得分:0)

你应该调整tableView标题的大小,也可以尝试下面的代码。

@IBAction func segmentAction(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        tableView.scrollEnabled = true
        segment = 0
        self.tableView.tableHeaderView!.hidden = false

        var newFrame =  self.tableView.tableHeaderView!.frame;
        newFrame.size.height = 44.0;  //defaut height whatever you want
        self.tableView.tableHeaderView!.frame = newFrame;
    case 1:
        tableView.scrollEnabled = false
        segment = 1
        self.tableView.tableHeaderView!.hidden = true

        var newFrame =  self.tableView.tableHeaderView!.frame;
        newFrame.size.height = 0.0;  //defaut height whatever you want
        self.tableView.tableHeaderView!.frame = newFrame;

    default:
        break
    }
    tableView.reloadData()

}